WriteEndObject() public method

public WriteEndObject ( System writer ) : void
writer System
return void
コード例 #1
0
        public void CompareXml()
        {
            var auditConfiguration = AuditConfiguration.Default;

            auditConfiguration.IncludeRelationships = true;
            auditConfiguration.LoadRelationships = true;
            auditConfiguration.DefaultAuditable = true;

            // customize the audit for Task entity
            //auditConfiguration.IsAuditable<Task>()
            //  .NotAudited(t => t.TaskExtended)
            //  .FormatWith(t => t.Status, v => FormatStatus(v));

            // set name as the display member when status is a foreign key
            auditConfiguration.IsAuditable<Status>()
              .DisplayMember(t => t.Name);

            var db = new TrackerContext();
            var audit = db.BeginAudit();

            var user = db.Users.Find(1);
            user.Comment = "Testing: " + DateTime.Now.Ticks;

            var task = new Task()
            {
                AssignedId = 1,
                CreatedId = 1,
                StatusId = 1,
                PriorityId = 2,
                Summary = "Summary: " + DateTime.Now.Ticks
            };
            db.Tasks.Add(task);

            var task2 = db.Tasks.Find(1);
            task2.PriorityId = 2;
            task2.StatusId = 2;
            task2.Summary = "Summary: " + DateTime.Now.Ticks;

            var log = audit.CreateLog();
            Assert.IsNotNull(log);

            string xml = log.ToXml();
            Assert.IsNotNull(xml);
            File.WriteAllText(@"test.xml.xml", xml);

            foreach (var property in log.Entities.SelectMany(e => e.Properties))
            {
                Assert.AreNotEqual(property.Current, "{error}");
                Assert.AreNotEqual(property.Original, "{error}");
            }

            var builder = new StringBuilder();
            var settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true };
            var writer = XmlWriter.Create(builder, settings);

            var serializer = new DataContractSerializer(typeof(AuditLog));
            serializer.WriteStartObject(writer, log);
            writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
            serializer.WriteObjectContent(writer, log);
            serializer.WriteEndObject(writer);

            writer.Flush();

            string xml2 = builder.ToString();
            File.WriteAllText(@"test.data.xml", xml2);

            string json = JsonConvert.SerializeObject(log, Newtonsoft.Json.Formatting.Indented, new StringEnumConverter());
            File.WriteAllText(@"test.data.json", json);


        }
コード例 #2
0
ファイル: VisitForm.cs プロジェクト: djdino56/PPS2_Fontys
        public bool SaveVisit()
        {
            if (_observations.Count != 0 && _currentVisit != null && _project != null)
            {
                // WRITING XML FILE
                var t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                var secondsSinceEpoch = (int)t.TotalSeconds;

                var filename = $@"project_{secondsSinceEpoch}.xml";
                using (var fw = new FileStream(path: filename, mode: FileMode.Create, access: FileAccess.Write))
                {
                    var dcsw = new DataContractSerializer(typeof (Xml));
                    var xmlWrite = new Xml(_currentVisit, _observations);

                    var writer = XmlDictionaryWriter.CreateTextWriter(fw);
                    writer.WriteStartDocument(true);
                    writer.WriteStartElement("Waarnemingen");
                    // Try to write and catch the exception if failed to write
                    try
                    {
                        dcsw.WriteObject(fw, xmlWrite);
                        dcsw.WriteEndObject(writer);
                        return true;
                    }
                    catch (Exception x)
                    {
                        //MessageBox.Show(x.ToString());
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }