WriteObjectContent() public méthode

public WriteObjectContent ( System writer, object graph ) : void
writer System
graph object
Résultat void
Exemple #1
0
        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("xmlns", "x", null, "http://www.w3.org/2001/XMLSchema");

            if (_Content != null)
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(StoredProcedureResponse));
                serializer.WriteObjectContent(writer, _Content);
            }
        }
 /// <summary>
 /// Write the header to a byte stream.
 /// </summary>
 /// <param name="header">The header.</param>
 /// <returns>The serialized data.</returns>
 private static byte[] Write(MiniProfilerResultsHeader header)
 {
     var serializer = new DataContractSerializer(typeof(MiniProfilerResultsHeader), HeaderName, HeaderNamespace);
     using (var stream = new MemoryStream())
     {
         using (var writer = new XmlTextWriter(stream, Encoding.Unicode))
         {
             writer.WriteStartElement(HeaderName, HeaderNamespace);
             serializer.WriteObjectContent(writer, header);
             writer.WriteEndElement();
             writer.Flush();
             byte[] buffer = stream.GetBuffer();
             Array.Resize(ref buffer, (int)stream.Length);
             return buffer;
         }
     }            
 }
Exemple #3
0
		void TestWriteHeaderContent (object value, string expected)
		{
			MessageHeader h = MessageHeader.CreateHeader ("foo", "bar", value);
			XmlObjectSerializer f = new DataContractSerializer (value.GetType ());

			StringBuilder sb = new StringBuilder ();
			XmlWriterSettings settings = new XmlWriterSettings ();
			settings.ConformanceLevel = ConformanceLevel.Auto;                        
			XmlDictionaryWriter w = XmlDictionaryWriter.CreateDictionaryWriter (
				XmlWriter.Create (sb, settings));

			w.WriteStartElement ("dummy-root");
			h.WriteHeaderContents (w, MessageVersion.Soap12WSAddressing10);
			w.WriteEndElement ();
			w.Flush ();
			string actual2 = sb.ToString ();

			sb.Length = 0;
			w.WriteStartElement ("dummy-root");
			f.WriteObjectContent (w, value);
			w.WriteEndElement ();
			w.Flush ();
			string actual1 = sb.ToString ();

			// the output of WriteHeaderContent is the same as XmlSerializer.Serialize
			Assert.AreEqual (expected, actual1, "#1");
			Assert.AreEqual (expected, actual2, "#2");
		}
        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);


        }