BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in XmlSerializer

 
 
Page 20363 of 21350

Cleaning up your Xml Serialization output

Blogger : MarkAllanson.Net
All posts : All posts by MarkAllanson.Net
Category : XmlSerializer
Blogged date : 2004 Dec 07

Serialization is a great thing, except when you can`t get it to do what you want it to do. Case in point: I was serializing a small class that was going to be farmed off to a logger, which took the data and stored it in a logging database sitting on SQL Server. If you have ever done any serialization before, you will know that when you use the System.Xml.XmlSerialization.XmlSerializer to serialize a class you get a number of artifacts in the resultant xml, something along the lines of that below: (note I was serializing a class called ContextItem) .cf { font-family: Verdana; font-size: 8pt; color: black; background: white; border-top: silver 1pt solid; padding-top: 0pt; border-left: navy 2pt solid; padding-left: 0pt; margin-left: 20pt; border-right: silver 1pt solid; padding-right: 0pt; margin-right: 20pt; border-bottom: silver 1pt solid; padding-bottom: 0pt; line-height:12pt;}.cln { color: teal; }.cb1 { color: blue; }.cb2 { color: maroon; }.cb3 { color: uchsia; }.cb4 { color: red; }    1     2  The artifacts are the xml processing instruction as the root node of the serialized class, and the declaration of the xsd and xsi namespaces within the ContextItem node. In the case above, serialization was done using an System.Xml.XmlTextWriter combined with the XmlSerializer, and the artifacts are due to a combination of the two working together. The Xml processing instruction is placed in the resultant output by the XmlTextReader, and the xsd and xsi namespace delcarations are placed in the resultant output by the serializer. .cf { font-family: Verdana; font-size: 8pt; color: black; background: white; border-top: silver 1pt solid; padding-top: 0pt; border-left: navy 2pt solid; padding-left: 0pt; margin-left: 20pt; border-right: silver 1pt solid; padding-right: 0pt; margin-right: 20pt; border-bottom: silver 1pt solid; padding-bottom: 0pt; }.cln { color: teal; }.cb1 { color: blue; }.cb2 { color: green; }   97     public static string Serialize(ContextItem i)   98     {   99         // Write the serialized result to a string builder  100         XmlSerializer serializer = new XmlSerializer(typeof(ContextItem));  101         StringBuilder sb = new StringBuilder();  102         StringWriter sw = new StringWriter(sb);  103         XmlTextWriter tw = new XmlTextWriter(sw);  104   105         // get rid of xml Processing Instruction declaration  106         tw.Formatting = Formatting.None;  107         tw.WriteRaw(String.Empty);   108   109         // get rid of xsd/xsi declaraions  110         XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();  111         xsn.Add(String.Empty, String.Empty);  112   113         // serialize and return the result  114         serializer.Serialize(tw, i, xsn);  115         return sb.ToString();  116     } Removing the xml processing instruction Seeings as the XmlTextReader is responsible for writing the xml processing instruction at the top of the above document the trick here is to fool it into not writing the xml processing instruction. This is done by lines 6 and 7 in the above code snippet. Setting the formatting just tells the XmlTextWriter not to apply any type of formatting to the output document (the serialized output will be one long string). Line 7 however, puts the XmlTextWriter current node into mixed mode (the node can contain both text and child xml nodes), therefore causing the xml processing instruction not to be output. Removing the xsd and xsi namespace declarations I am not sure if the method I have used to remove the xsd and xsi declaraions is actually embedded in the implementation of the XmlSerializerNamespaces class or if it is an unintended side effect of the implementation. Maybe Dare could clear this up An overloaded method of the XmlSerializer.Serialize() function takes a third argument, specifically an XmlSerializerNamespaces object which is normally used as a container for a collection of namespaces and prefixes to use in the serialization of the object. To remove the xsi and xsd namespace declarations you need to create an empty XmlSerializerNamespaces class and add a single entry, specifying an empty namespace prefix and namespace uri as shown in lines 110-111 in the above sample. Output after these two operations cleans up the xml nicely, ready for logging into a SQL server thousands of times if required. Final output is shown below. .cf { font-family: Verdana; font-size: 8pt; color: black; background: white; border-top: silver 1pt solid; padding-top: 0pt; border-left: navy 2pt solid; padding-left: 0pt; margin-left: 20pt; border-right: silver 1pt solid; padding-right: 0pt; margin-right: 20pt; border-bottom: silver 1pt solid; padding-bottom: 0pt; }.cln { color: teal; }.cb1 { color: blue; }.cb2 { color: maroon; }.cb3 { color: fuchsia; }.cb4 { color: red; }    1 ...

Read comments or post a reply to : Cleaning up your Xml Serialization output
Page 20363 of 21350

Newest posts
 

    Email TopXML