<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>umop apisdn &#187; .NET</title> <atom:link href="http://pournaras.zilsen.com/category/net/feed/" rel="self" type="application/rss+xml" /><link>http://pournaras.zilsen.com</link> <description>Thoughts, comments, guides and discoveries</description> <lastBuildDate>Mon, 21 Nov 2011 15:24:17 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>XML serialization ouput my way</title><link>http://pournaras.zilsen.com/2009/05/20/xml-serialization-ouput-my-way/</link> <comments>http://pournaras.zilsen.com/2009/05/20/xml-serialization-ouput-my-way/#comments</comments> <pubDate>Wed, 20 May 2009 12:02:38 +0000</pubDate> <dc:creator>Takis</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[serialization]]></category> <category><![CDATA[xml]]></category> <guid
isPermaLink="false">http://pournaras.zilsen.com/?p=141</guid> <description><![CDATA[The default XML serialization adds the xml header and a couple of namespace definitions &#8211; elements which aren&#8217;t always welcome. Here&#8217;s how to control the XML source. Let&#8217;s start with a simple class called &#8216;Book&#8217;: Public Class Book Public Title As String = String.Empty Public Sub New() End Sub Public Sub New(ByVal title As String) [...]]]></description> <content:encoded><![CDATA[<p>The default XML serialization adds the xml header and a couple of namespace definitions &#8211; elements which aren&#8217;t always welcome. Here&#8217;s how to control the XML source.</p><p>Let&#8217;s start with a simple class called &#8216;Book&#8217;:</p><pre class="brush: vbnet">
Public Class Book
    Public Title As String = String.Empty
    Public Sub New()
    End Sub
    Public Sub New(ByVal title As String)
        Me.Title = title
    End Sub
End Class
</pre><p>Our mini-project (in this case: a console application) should be able to create a new instance of the book class and serialize it:</p><pre class="brush: vbnet">
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Module Main
    Sub Main()
        Dim book As New Book(&quot;XML Serialization&quot;)
        Dim result As String
        Dim serializer As New XmlSerializer(GetType(Book))
        Using writer As New StringWriter()
            serializer.Serialize(writer, book)
            result = writer.ToString
        End Using
        Console.WriteLine(result)
        Console.ReadLine()
    End Sub
End Module
</pre><p>The result looks like this:</p><pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;
&lt;Book
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
  &lt;Title&gt;XML Serialization&lt;/Title&gt;
&lt;/Book&gt;
</pre><h4>Task #1: removing the namespace definitions</h4><p>In order to remove the namespace definitions, we need to define a new set of namespaces and tell our serializer to use this set. We can do this by using the XmlSerializerNamespaces class. We need to add the following lines just after the XmlSerializer definition:</p><pre class="brush: vbnet">
Dim nameSpaces As XmlSerializerNamespaces = _
    New XmlSerializerNamespaces()
nameSpaces.Add(&quot;&quot;, &quot;&quot;)
</pre><p>and we also need to replace line 15 in the original code with the following statement:</p><pre class="brush: vbnet">
serializer.Serialize(writer, book, nameSpaces)
</pre><p>This way our XML source becomes:</p><pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;
&lt;Book&gt;
  &lt;Title&gt;XML Serialization&lt;/Title&gt;
&lt;/Book&gt;
</pre><h4>Task #2: Removing the XML header</h4><p>Next thing on the to-do list is to find a way to produce a plain XML file without any definitions whatsoever. To accomplish this, we need to further modify our code and use the XmlWriterSettings class with OmitXmlDeclaration property set to true. Replace lines 14-18 with the following:</p><pre class="brush: vbnet">
Dim settings As New XmlWriterSettings()
settings.OmitXmlDeclaration = True
settings.Indent = True
Using writer As New StringWriter()
  Using xmlWriter As XmlWriter = _
      xmlWriter.Create(writer, settings)
    serializer.Serialize(xmlWriter, book, nameSpaces)
    result = writer.ToString
  End Using
End Using
</pre><p>Our XML now becomes:</p><pre class="brush: xml">
&lt;Book&gt;
  &lt;Title&gt;XML Serialization&lt;/Title&gt;
&lt;/Book&gt;
</pre><h4>Task #3: adding our own namespace definition</h4><p>Our final task is to insert our own namespace definition (and only that one). First thing to do is to instantiate the XmlSerializer with our own namespace. Then we need to add our namespace to our custom namespace list. Replace the serializer definition with:</p><pre class="brush: vbnet">
Dim serializer As New XmlSerializer(GetType(Book), _
    &quot;urn:our_namespace&quot;)
</pre><p>add change the nameSpaces.add statement with:</p><pre class="brush: vbnet">
nameSpaces.Add(&quot;&quot;, &quot;urn:our_namespace&quot;)
</pre><p>The last set of changes produce the following XML:</p><pre class="brush: xml">
&lt;Book xmlns=&quot;urn:our_namespace&quot;&gt;
  &lt;Title&gt;XML Serialization&lt;/Title&gt;
&lt;/Book&gt;
</pre><p>There you have it.</p> ]]></content:encoded> <wfw:commentRss>http://pournaras.zilsen.com/2009/05/20/xml-serialization-ouput-my-way/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
