<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Dan Miser - XML</title>
    <link>http://www.distribucon.com/blog/</link>
    <description>Thoughts from Dan Miser</description>
    <language>en-us</language>
    <copyright>Dan Miser</copyright>
    <lastBuildDate>Tue, 29 Jan 2008 18:23:57 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>dmiser@distribucon.com</managingEditor>
    <webMaster>dmiser@distribucon.com</webMaster>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=819d903f-b3c6-448e-ba57-83ec077dd150</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,819d903f-b3c6-448e-ba57-83ec077dd150.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,819d903f-b3c6-448e-ba57-83ec077dd150.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=819d903f-b3c6-448e-ba57-83ec077dd150</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have a .NET application that generates an XML file via serialization (through XmlTextWriter)
and submits the data via https. Recently, they changed something on their end to only
accept an upper cased UTF-8 encoding, like this:
</p>
        <pre>
          <code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; </code>
        </pre>
        <p>
Unfortunately, using the .NET classes mentioned above, it generates the encoding string
in lower case. I could find one mention of this on google, and they said to have the
other company change (not an option here), or override XmlTextWriter. I went with
that approach, making heavy use of <a href="http://www.aisto.com/roeder/dotnet" target="_blank">Reflector</a> along
the way. I started by looking at the WriteStartDocument method, but realized that
the private StartDocument is the thing that generates the output. It also gets called
by the overloaded WriteStartDocument, so I'd need to override that method, too. However,
in the private StartDocument method, it uses a bunch of private variables and generates
output via another private method, InternalWriteProcessingInstructions. Ugly. At this
point I realize that XmlTextWriter is not a class made for inheriting. 
</p>
        <p>
Back to Reflector, and I notice that the Encoding.WebName is the property used to
write out the encoding string. I now create a descendant class of UTF8Encoding. The
class is listed below. Now I just call XmlTextWriter, passing in UpperCaseUTF8Encoding.UpperCaseUTF8
for the Encoding type, and everything works perfectly. 
</p>
        <pre class="code">
          <span style="color: rgb(0,0,255)">public</span>
          <span style="color: rgb(0,0,255)">class</span>
          <span style="color: rgb(43,145,175)">UpperCaseUTF8Encoding</span> : <span style="color: rgb(43,145,175)">UTF8Encoding </span> { <span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,255)">override</span><span style="color: rgb(0,0,255)">string</span> WebName
{ <span style="color: rgb(0,0,255)">get</span> { <span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,255)">base</span>.WebName.ToUpper();
} } <span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(43,145,175)">UpperCaseUTF8Encoding</span> UpperCaseUTF8
{ <span style="color: rgb(0,0,255)">get </span> { <span style="color: rgb(0,0,255)">if</span> (upperCaseUtf8Encoding
== <span style="color: rgb(0,0,255)">null</span>) upperCaseUtf8Encoding = <span style="color: rgb(0,0,255)">new</span><span style="color: rgb(43,145,175)">UpperCaseUTF8Encoding</span>(); <span style="color: rgb(0,0,255)">return</span> upperCaseUtf8Encoding;
} } <span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(43,145,175)">UpperCaseUTF8Encoding</span> upperCaseUtf8Encoding
= <span style="color: rgb(0,0,255)">null</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
          <a href="http://11011.net/software/vspaste">
            <img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=819d903f-b3c6-448e-ba57-83ec077dd150" />
          </a>
        </a>
      </body>
      <title>Making the Encoding property upper case during XmlSerialization</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,819d903f-b3c6-448e-ba57-83ec077dd150.aspx</guid>
      <link>http://www.distribucon.com/blog/MakingTheEncodingPropertyUpperCaseDuringXmlSerialization.aspx</link>
      <pubDate>Tue, 29 Jan 2008 18:23:57 GMT</pubDate>
      <description>&lt;p&gt;
I have a .NET application that generates an XML file via serialization (through XmlTextWriter)
and submits the data via https. Recently, they changed something on their end to only
accept an upper cased UTF-8 encoding, like this:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; &amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt; &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Unfortunately, using the .NET classes mentioned above, it generates the encoding string
in lower case. I could find one mention of this on google, and they said to have the
other company change (not an option here), or override XmlTextWriter. I went with
that approach, making heavy use of &lt;a href="http://www.aisto.com/roeder/dotnet" target="_blank"&gt;Reflector&lt;/a&gt; along
the way. I started by looking at the WriteStartDocument method, but realized that
the private StartDocument is the thing that generates the output. It also gets called
by the overloaded WriteStartDocument, so I'd need to override that method, too. However,
in the private StartDocument method, it uses a bunch of private variables and generates
output via another private method, InternalWriteProcessingInstructions. Ugly. At this
point I realize that XmlTextWriter is not a class made for inheriting. 
&lt;p&gt;
Back to Reflector, and I notice that the Encoding.WebName is the property used to
write out the encoding string. I now create a descendant class of UTF8Encoding. The
class is listed below. Now I just call XmlTextWriter, passing in UpperCaseUTF8Encoding.UpperCaseUTF8
for the Encoding type, and everything works perfectly. &lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;UpperCaseUTF8Encoding&lt;/span&gt; : &lt;span style="color: rgb(43,145,175)"&gt;UTF8Encoding &lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; WebName
{ &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;base&lt;/span&gt;.WebName.ToUpper();
} } &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;UpperCaseUTF8Encoding&lt;/span&gt; UpperCaseUTF8
{ &lt;span style="color: rgb(0,0,255)"&gt;get &lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (upperCaseUtf8Encoding
== &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;) upperCaseUtf8Encoding = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;UpperCaseUTF8Encoding&lt;/span&gt;(); &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; upperCaseUtf8Encoding;
} } &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;UpperCaseUTF8Encoding&lt;/span&gt; upperCaseUtf8Encoding
= &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=819d903f-b3c6-448e-ba57-83ec077dd150" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,819d903f-b3c6-448e-ba57-83ec077dd150.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=ecaf0c3a-f630-4fc9-8a20-b53fac515c8f</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,ecaf0c3a-f630-4fc9-8a20-b53fac515c8f.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,ecaf0c3a-f630-4fc9-8a20-b53fac515c8f.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=ecaf0c3a-f630-4fc9-8a20-b53fac515c8f</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have been reading Dustin Campbell's series, <a href="http://diditwith.net/2007/12/29/TwelveDaysOfRefactorXmasDayTenRefactoringInXMLLiterals.aspx" target="_blank">The
12 Days of Refactor! X-mas</a>, with interest. He's a very good writer, and has broken
down a bunch of Developer Express features into easy to digest chunks of information.
</p>
        <p>
The episode I linked to above touched on the <a href="http://msdn2.microsoft.com/en-us/library/ms364068%28vs.80%29.aspx#vb9overview_topic6" target="_blank">XML
Literal</a> support that shipped with VB in Visual Studio 2008. The one sentence summary
of this technology is that it lets the VB compiler translate raw XML in the source
code into strongly-typed code using XML classes under the hood. It's a very nice idea,
and I encourage you to look at the links to learn a little more about it.
</p>
        <p>
While the VB team thought this was a good idea and added the feature, Anders and the
C# team believed that it was superfluous to add such a thing to the C# side. I haven't
really made up my mind which group I side with yet. However, Anders is benevolent
:-), so he gave us the little-documented PasteXmlAsLinq addin that will take XML on
the clipboard and paste it into a file in C# syntax building the XML up using classes
like XElement.
</p>
        <p>
In a default install of VS2008, open C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip
and extract the LinqSamples\PasteXmlAsLinq files. There is a Readme.html file in there
that tells you how to build, install and use the addin. 
</p>
        <p>
While this isn't the same as VB's support for XML Literals, it does make things easier
to work with. 
</p>
        <img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=ecaf0c3a-f630-4fc9-8a20-b53fac515c8f" />
      </body>
      <title>VB XML Literal support in C#</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,ecaf0c3a-f630-4fc9-8a20-b53fac515c8f.aspx</guid>
      <link>http://www.distribucon.com/blog/VBXMLLiteralSupportInC.aspx</link>
      <pubDate>Sun, 30 Dec 2007 23:02:25 GMT</pubDate>
      <description>&lt;p&gt;
I have been reading Dustin Campbell's series, &lt;a href="http://diditwith.net/2007/12/29/TwelveDaysOfRefactorXmasDayTenRefactoringInXMLLiterals.aspx" target="_blank"&gt;The
12 Days of Refactor! X-mas&lt;/a&gt;, with interest. He's a very good writer, and has broken
down a bunch of Developer Express features into easy to digest chunks of information.
&lt;/p&gt;
&lt;p&gt;
The episode I linked to above touched on the &lt;a href="http://msdn2.microsoft.com/en-us/library/ms364068%28vs.80%29.aspx#vb9overview_topic6" target="_blank"&gt;XML
Literal&lt;/a&gt; support that shipped with VB in Visual Studio 2008. The one sentence summary
of this technology is that it lets the VB compiler translate raw XML in the source
code into strongly-typed code using XML classes under the hood. It's a very nice idea,
and I encourage you to look at the links to learn a little more about it.
&lt;/p&gt;
&lt;p&gt;
While the VB team thought this was a good idea and added the feature, Anders and the
C# team believed that it was superfluous to add such a thing to the C# side. I haven't
really made up my mind which group I side with yet. However, Anders is benevolent
:-), so he gave us the little-documented PasteXmlAsLinq addin that will take XML on
the clipboard and paste it into a file in C# syntax building the XML up using classes
like XElement.
&lt;/p&gt;
&lt;p&gt;
In a default install of VS2008, open C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip
and extract the LinqSamples\PasteXmlAsLinq files. There is a Readme.html file in there
that tells you how to build, install and use the addin. 
&lt;/p&gt;
&lt;p&gt;
While this isn't the same as VB's support for XML Literals, it does make things easier
to work with. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=ecaf0c3a-f630-4fc9-8a20-b53fac515c8f" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,ecaf0c3a-f630-4fc9-8a20-b53fac515c8f.aspx</comments>
      <category>.NET</category>
      <category>LINQ</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=fef887de-adfd-4a25-894b-5f57838ed9aa</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,fef887de-adfd-4a25-894b-5f57838ed9aa.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,fef887de-adfd-4a25-894b-5f57838ed9aa.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=fef887de-adfd-4a25-894b-5f57838ed9aa</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I read Scott Hanselman's well-written article <a href="http://www.hanselman.com/blog/HOWTODebugIntoANETXmlSerializerGeneratedAssembly.aspx" target="_new">HOW
TO: Debug into a .NET XmlSerializer Generated Assembly</a>. I can add a few more little
(and most likely obvious) things to his article:<br /><ul><li>
This still works in .NET 2.0. 
</li><li>
If you just do an Add New Item to add a new Application Configuration File to your
project in VS, you can just paste the debugging flags in, and the config file will
get deployed to the application directory automatically. 
</li><li>
You can just Step Into (F11) the call to x.Serialize, and you will be taken into the
proper generated file. You may want to open the file manually as Scott mentioned,
but I find it to be faster to press F11, and then go to the place in the file I want
to debug. 
</li></ul><p>
In the end, we ended up <a href="http://www.zorched.net/2007/02/28/custom-xml-serialization-for-the-net-compact-framework/" target="_new">writing
our own serialization for .NET CF</a> due to one too many bugs.
</p><img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=fef887de-adfd-4a25-894b-5f57838ed9aa" /></body>
      <title>Debugging XMLSerializer generated code</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,fef887de-adfd-4a25-894b-5f57838ed9aa.aspx</guid>
      <link>http://www.distribucon.com/blog/DebuggingXMLSerializerGeneratedCode.aspx</link>
      <pubDate>Thu, 08 Mar 2007 20:26:21 GMT</pubDate>
      <description>I read Scott Hanselman's well-written article &lt;a href="http://www.hanselman.com/blog/HOWTODebugIntoANETXmlSerializerGeneratedAssembly.aspx" target=_new&gt;HOW
TO: Debug into a .NET XmlSerializer Generated Assembly&lt;/a&gt;. I can add a few more little
(and most likely obvious) things to his article:&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
This still works in .NET 2.0. 
&lt;li&gt;
If you just do an Add New Item to add a new Application Configuration File to your
project in VS, you can just paste the debugging flags in, and the config file will
get deployed to the application directory automatically. 
&lt;li&gt;
You can just Step Into (F11) the call to x.Serialize, and you will be taken into the
proper generated file. You may want to open the file manually as Scott mentioned,
but I find it to be faster to press F11, and then go to the place in the file I want
to debug. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
In the end, we ended up &lt;a href="http://www.zorched.net/2007/02/28/custom-xml-serialization-for-the-net-compact-framework/" target=_new&gt;writing
our own serialization for .NET CF&lt;/a&gt; due to one too many bugs.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=fef887de-adfd-4a25-894b-5f57838ed9aa" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,fef887de-adfd-4a25-894b-5f57838ed9aa.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=9f651ae9-101e-4b62-804d-37d72295da89</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,9f651ae9-101e-4b62-804d-37d72295da89.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,9f651ae9-101e-4b62-804d-37d72295da89.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9f651ae9-101e-4b62-804d-37d72295da89</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">XML serialization is handy, but it does
lack some power. For example, if you have class inheritance, and you want a property
of the child class to have a different name than the one given to it by the parent,
you will get an error at run-time when trying to serialize the object: "Member 'Derived.Name'
hides inherited member 'Base.Name', but has different custom attributes.". 
<p>
Assume you have a class structure like this:<br /></p><code><!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20     \cf3 public\cf0  \cf3 abstract\cf0  \cf3 class\cf0  \cf4 Base\par ??\cf0     \{\par ??        \cf3 private\cf0  \cf3 int\cf0  id;\par ??        \cf3 private\cf0  \cf3 string\cf0  name;\par ??\par ??        [XmlElement(\cf5 "id"\cf0 )]\par ??        \cf3 public\cf0  \cf3 int\cf0  Id\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  id; \}\par ??            \cf3 set\cf0  \{ id = \cf3 value\cf0 ; \}\par ??        \}\par ??\par ??        [XmlElement(\cf5 "name"\cf0 )]\par ??        \cf3 public\cf0  \cf3 virtual\cf0  \cf3 string\cf0  Name\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  name; \}\par ??            \cf3 set\cf0  \{ name = \cf3 value\cf0 ; \}\par ??        \}\par ??    \}\par ??\par ??    \cf3 public\cf0  \cf3 class\cf0  \cf4 Derived\cf0  : \cf4 Base\par ??\cf0     \{\par ??        [XmlElement(\cf5 "newName"\cf0 )]\par ??        \cf3 public\cf0  \cf3 override\cf0  \cf3 string\cf0  Name\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  \cf3 base\cf0 .Name; \}\par ??            \cf3 set\cf0  \{ \cf3 base\cf0 .Name = \cf3 value\cf0 ; \}\par ??        \}\par ??    \}\par ??}
--><pre style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px"><span style="COLOR: blue">public</span><span style="COLOR: blue">abstract</span><span style="COLOR: blue">class</span><span style="COLOR: #2b91af">Base</span></p><p style="MARGIN: 0px">
{
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">private</span><span style="COLOR: blue">int</span> id;
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">private</span><span style="COLOR: blue">string</span> name;
</p><p style="MARGIN: 0px">
 
</p><p style="MARGIN: 0px">
    [XmlElement(<span style="COLOR: #a31515">"id"</span>)]
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span><span style="COLOR: blue">int</span> Id
</p><p style="MARGIN: 0px">
    {
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">get</span> { <span style="COLOR: blue">return</span> id;
}
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">set</span> { id = <span style="COLOR: blue">value</span>;
}
</p><p style="MARGIN: 0px">
    }
</p><p style="MARGIN: 0px">
 
</p><p style="MARGIN: 0px">
    [XmlElement(<span style="COLOR: #a31515">"name"</span>)]
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span><span style="COLOR: blue">virtual</span><span style="COLOR: blue">string</span> Name
</p><p style="MARGIN: 0px">
    {
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">get</span> { <span style="COLOR: blue">return</span> name;
}
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">set</span> { name
= <span style="COLOR: blue">value</span>; }
</p><p style="MARGIN: 0px">
    }
</p><p style="MARGIN: 0px">
}
</p><p style="MARGIN: 0px">
 
</p><p style="MARGIN: 0px"><span style="COLOR: blue">public</span><span style="COLOR: blue">class</span><span style="COLOR: #2b91af">Derived</span> : <span style="COLOR: #2b91af">Base</span></p><p style="MARGIN: 0px">
{
</p><p style="MARGIN: 0px">
    [XmlElement(<span style="COLOR: #a31515">"newName"</span>)]
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span><span style="COLOR: blue">override</span><span style="COLOR: blue">string</span> Name
</p><p style="MARGIN: 0px">
    {
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">get</span> { <span style="COLOR: blue">return</span><span style="COLOR: blue">base</span>.Name;
}
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">set</span> { <span style="COLOR: blue">base</span>.Name
= <span style="COLOR: blue">value</span>; }
</p><p style="MARGIN: 0px">
    }
</p><p style="MARGIN: 0px">
}
</p></pre><p></p></code>To see the error, call it like this:<br /><code><span style="COLOR: blue"></span></code><p><code><span style="COLOR: blue">static</span><span style="COLOR: blue">void</span> Main(<span style="COLOR: blue">string</span>[]
args)
</code></p><pre><div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px">
{
</p><p style="MARGIN: 0px">
    <span style="COLOR: #2b91af">Derived</span> d = <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">Derived</span>();
</p><p style="MARGIN: 0px">
    d.Id = 23;
</p><p style="MARGIN: 0px">
    d.Name = <span style="COLOR: #a31515">"Dan"</span>;
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">string</span> xml = GetXMLFromObject(d);
</p><p style="MARGIN: 0px">
    <span style="COLOR: #2b91af">Console</span>.WriteLine(xml);
</p><p style="MARGIN: 0px">
}
</p></div></pre><p>
Download the sample application <a href="http://www.distribucon.com/dotnet/AbstractError.zip" target="_new">here</a> to
play with it.
</p><img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=9f651ae9-101e-4b62-804d-37d72295da89" /></body>
      <title>Overriding XmlElement names and XML Serialization</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,9f651ae9-101e-4b62-804d-37d72295da89.aspx</guid>
      <link>http://www.distribucon.com/blog/OverridingXmlElementNamesAndXMLSerialization.aspx</link>
      <pubDate>Wed, 10 Jan 2007 17:57:12 GMT</pubDate>
      <description>XML serialization is handy, but it does lack some power. For example, if you have class inheritance, and you want a property of the child class to have a different name than the one given to it by the parent, you will get an error at run-time when trying to serialize the object: "Member 'Derived.Name' hides inherited member 'Base.Name', but has different custom attributes.". 
&lt;p&gt;
Assume you have a class structure like this:&lt;br&gt;
&lt;/p&gt;
&lt;code&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20     \cf3 public\cf0  \cf3 abstract\cf0  \cf3 class\cf0  \cf4 Base\par ??\cf0     \{\par ??        \cf3 private\cf0  \cf3 int\cf0  id;\par ??        \cf3 private\cf0  \cf3 string\cf0  name;\par ??\par ??        [XmlElement(\cf5 "id"\cf0 )]\par ??        \cf3 public\cf0  \cf3 int\cf0  Id\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  id; \}\par ??            \cf3 set\cf0  \{ id = \cf3 value\cf0 ; \}\par ??        \}\par ??\par ??        [XmlElement(\cf5 "name"\cf0 )]\par ??        \cf3 public\cf0  \cf3 virtual\cf0  \cf3 string\cf0  Name\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  name; \}\par ??            \cf3 set\cf0  \{ name = \cf3 value\cf0 ; \}\par ??        \}\par ??    \}\par ??\par ??    \cf3 public\cf0  \cf3 class\cf0  \cf4 Derived\cf0  : \cf4 Base\par ??\cf0     \{\par ??        [XmlElement(\cf5 "newName"\cf0 )]\par ??        \cf3 public\cf0  \cf3 override\cf0  \cf3 string\cf0  Name\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  \cf3 base\cf0 .Name; \}\par ??            \cf3 set\cf0  \{ \cf3 base\cf0 .Name = \cf3 value\cf0 ; \}\par ??        \}\par ??    \}\par ??}
--&gt;&lt;pre style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Base&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; id;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlElement(&lt;span style="COLOR: #a31515"&gt;"id"&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; Id
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; id;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; { id = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlElement(&lt;span style="COLOR: #a31515"&gt;"name"&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Name
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; name;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; { name
= &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Derived&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;Base&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlElement(&lt;span style="COLOR: #a31515"&gt;"newName"&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Name
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Name;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Name
= &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
}
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;/code&gt;To see the error, call it like this:&lt;br&gt;
&lt;code&gt;&lt;span style="COLOR: blue"&gt;&lt;/span&gt;&lt;/code&gt;&gt;
&lt;p&gt;
&lt;code&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[]
args)
&lt;/p&gt;
&lt;pre&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;Derived&lt;/span&gt; d = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Derived&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; d.Id = 23;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; d.Name = &lt;span style="COLOR: #a31515"&gt;"Dan"&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; xml = GetXMLFromObject(d);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(xml);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&gt;&lt;/pre&gt;
&lt;p&gt;
Download the sample application &lt;a href="http://www.distribucon.com/dotnet/AbstractError.zip" target=_new&gt;here&lt;/a&gt; to
play with it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=9f651ae9-101e-4b62-804d-37d72295da89" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,9f651ae9-101e-4b62-804d-37d72295da89.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=2012e4a9-1e45-4f6f-ac55-6b9e458017f7</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,2012e4a9-1e45-4f6f-ac55-6b9e458017f7.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,2012e4a9-1e45-4f6f-ac55-6b9e458017f7.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=2012e4a9-1e45-4f6f-ac55-6b9e458017f7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Some times, it is better to have a minimal
format for your XML file. When using XmlSerializer, it likes to add the standard XML
namespaces to the generated XML file. If you want those gone, use code like this:<br /><pre><code><!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red0\green128\blue0;}??\fs20         \cf3 public\cf0  \cf3 static\cf0  \cf3 string\cf0  CreateXMLFromObject(\cf3 object\cf0  obj, \cf3 string\cf0  xmlRoot)\par ??        \{\par ??            \cf4 XmlSerializer\cf0  xs = \cf3 new\cf0  \cf4 XmlSerializer\cf0 (obj.GetType());\par ??\par ??            \cf5 // Set up chain of writers\par ??\cf0             \cf4 StringBuilder\cf0  sb = \cf3 new\cf0  \cf4 StringBuilder\cf0 ();\par ??            \cf3 using\cf0  (\cf4 XmlTextWriter\cf0  writer = \cf3 new\cf0  \cf4 XmlTextWriter\cf0 (\cf3 new\cf0  \cf4 StringWriter\cf0 (sb)))\par ??            \{\par ??                writer.Formatting = \cf4 Formatting\cf0 .Indented;\par ??\par ??                \cf5 // Remove standard namespace attributes\par ??\cf0                 \cf4 XmlSerializerNamespaces\cf0  xsn = \cf3 new\cf0  \cf4 XmlSerializerNamespaces\cf0 ();\par ??                xsn.Add(\cf3 string\cf0 .Empty, \cf3 string\cf0 .Empty);\par ??\par ??                xs.Serialize(writer, obj, xsn);\par ??            \}\par ??\par ??            \cf3 return\cf0  sb.ToString();\par ??        \}\par ??}
--><div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px"><span style="COLOR: blue">public</span><span style="COLOR: blue">static</span><span style="COLOR: blue">string</span> GetXMLFromObject(<span style="COLOR: blue">object</span> obj)
</p><p style="MARGIN: 0px">
{
</p><p style="MARGIN: 0px">
    <span style="COLOR: #2b91af">XmlSerializer</span> xs = <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">XmlSerializer</span>(obj.GetType());
</p><p style="MARGIN: 0px">
 
</p><p style="MARGIN: 0px">
    <span style="COLOR: green">// Set up chain of writers</span></p><p style="MARGIN: 0px">
    <span style="COLOR: #2b91af">StringBuilder</span> sb = <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">StringBuilder</span>();
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">using</span> (<span style="COLOR: #2b91af">XmlTextWriter</span> writer
= <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">XmlTextWriter</span>(<span style="COLOR: blue">new</span><span style="COLOR: #2b91af">StringWriter</span>(sb)))
</p><p style="MARGIN: 0px">
    {
</p><p style="MARGIN: 0px">
        writer.Formatting = <span style="COLOR: #2b91af">Formatting</span>.Indented;
</p><p style="MARGIN: 0px">
 
</p><p style="MARGIN: 0px">
        <span style="COLOR: green">// Remove standard
namespace attributes</span></p><p style="MARGIN: 0px">
        <span style="COLOR: #2b91af">XmlSerializerNamespaces</span> xsn
= <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">XmlSerializerNamespaces</span>();
</p><p style="MARGIN: 0px">
        xsn.Add(<span style="COLOR: blue">string</span>.Empty, <span style="COLOR: blue">string</span>.Empty);
</p><p style="MARGIN: 0px">
        xs.Serialize(writer, obj, xsn);
</p><p style="MARGIN: 0px">
    }
</p><p style="MARGIN: 0px">
 
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">return</span> sb.ToString();
</p><p style="MARGIN: 0px">
}
</p></div></code></pre><p>
The key is to create an <a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx" target="_new">XmlSerializerNameSpaces</a> object,
populate it with empty strings, and then pass it to the XmlSerizliazer. The result
is a more compact XML file. Note that this works, despite the comment in the documentation
explicitly saying that it isn't supported. 
</p><p>
You can also use this class to add your own custom namespaces. This could be useful
to prefix certain elements with a certain namespace.
</p><img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=2012e4a9-1e45-4f6f-ac55-6b9e458017f7" /></body>
      <title>XML Namespaces in serialization</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,2012e4a9-1e45-4f6f-ac55-6b9e458017f7.aspx</guid>
      <link>http://www.distribucon.com/blog/XMLNamespacesInSerialization.aspx</link>
      <pubDate>Tue, 09 Jan 2007 15:52:45 GMT</pubDate>
      <description>Some times, it is better to have a minimal format for your XML file. When using XmlSerializer, it likes to add the standard XML namespaces to the generated XML file. If you want those gone, use code like this:&lt;br&gt;
&lt;pre&gt;&lt;code&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red0\green128\blue0;}??\fs20         \cf3 public\cf0  \cf3 static\cf0  \cf3 string\cf0  CreateXMLFromObject(\cf3 object\cf0  obj, \cf3 string\cf0  xmlRoot)\par ??        \{\par ??            \cf4 XmlSerializer\cf0  xs = \cf3 new\cf0  \cf4 XmlSerializer\cf0 (obj.GetType());\par ??\par ??            \cf5 // Set up chain of writers\par ??\cf0             \cf4 StringBuilder\cf0  sb = \cf3 new\cf0  \cf4 StringBuilder\cf0 ();\par ??            \cf3 using\cf0  (\cf4 XmlTextWriter\cf0  writer = \cf3 new\cf0  \cf4 XmlTextWriter\cf0 (\cf3 new\cf0  \cf4 StringWriter\cf0 (sb)))\par ??            \{\par ??                writer.Formatting = \cf4 Formatting\cf0 .Indented;\par ??\par ??                \cf5 // Remove standard namespace attributes\par ??\cf0                 \cf4 XmlSerializerNamespaces\cf0  xsn = \cf3 new\cf0  \cf4 XmlSerializerNamespaces\cf0 ();\par ??                xsn.Add(\cf3 string\cf0 .Empty, \cf3 string\cf0 .Empty);\par ??\par ??                xs.Serialize(writer, obj, xsn);\par ??            \}\par ??\par ??            \cf3 return\cf0  sb.ToString();\par ??        \}\par ??}
--&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; GetXMLFromObject(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; obj)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;XmlSerializer&lt;/span&gt; xs = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;XmlSerializer&lt;/span&gt;(obj.GetType());
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// Set up chain of writers&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;StringBuilder&lt;/span&gt; sb = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;StringBuilder&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: #2b91af"&gt;XmlTextWriter&lt;/span&gt; writer
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;XmlTextWriter&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;StringWriter&lt;/span&gt;(sb)))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Formatting = &lt;span style="COLOR: #2b91af"&gt;Formatting&lt;/span&gt;.Indented;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// Remove standard
namespace attributes&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;XmlSerializerNamespaces&lt;/span&gt; xsn
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;XmlSerializerNamespaces&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; xsn.Add(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Empty, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Empty);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; xs.Serialize(writer, obj, xsn);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; sb.ToString();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The key is to create an &lt;a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx" target=_new&gt;XmlSerializerNameSpaces&lt;/a&gt; object,
populate it with empty strings, and then pass it to the XmlSerizliazer. The result
is a more compact XML file. Note that this works, despite the comment in the documentation
explicitly saying that it isn't supported. 
&lt;p&gt;
You can also use this class to add your own custom namespaces. This could be useful
to prefix certain elements with a certain namespace.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=2012e4a9-1e45-4f6f-ac55-6b9e458017f7" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,2012e4a9-1e45-4f6f-ac55-6b9e458017f7.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=6bb6b064-6d03-4485-949c-04e679e51f5f</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,6bb6b064-6d03-4485-949c-04e679e51f5f.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,6bb6b064-6d03-4485-949c-04e679e51f5f.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6bb6b064-6d03-4485-949c-04e679e51f5f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">While the <a href="http://www.distribucon.com/blog/PermaLink,guid,35155273-aa80-4d03-a871-243b89a23f9b.aspx" traget="_new">XmlIgnore/Specified</a> pattern
to control serialization is very useful, it could be overkill for simpler serialization
scenarios. 
<p>
When you create an object with a bool field, that field is initialized and defaulted
to the value of false. Under normal conditions, this value will get serialized every
time. However, what if we only care about capturing that value when the value is true?
It turns out that we can use <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx" target="_new">DefaultValueAttribute</a>.
The first thing that may stand out to you is that this attribute lives in the System.ComponentModel
namespace, and not in an XML namespace. The online help also mentions that this is
used for visual designers and code generators. XmlSerializer also uses this attribute
to decide whether or not to stream the value during serialization, since it's really
a form of a code generator (more on that in a later post). If the value of the field
or property matches the value that you specify in the DefaultValueAttribute, then
that property will not be streamed. 
</p><p>
Using an <a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx" target="_new">XmlAttributeAttribute</a>,
you can specify that a certain property will be expressed as an XML attribute instead
of an XML element. To demonstrate both of these concepts, see the following code:<br /></p><pre><code><!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20     \cf3 public\cf0  \cf3 class\cf0  \cf4 Url\par ??\cf0     \{\par ??        [XmlAttribute(\cf5 "address"\cf0 )]\par ??        \cf3 public\cf0  \cf3 string\cf0  Address;\par ??\par ??        [XmlAttribute(\cf5 "primary"\cf0 ), \cf4 DefaultValue\cf0 (\cf3 false\cf0 )]\par ??        \cf3 public\cf0  \cf3 bool\cf0  Primary;\par ??\par ??        \cf3 public\cf0  Url()\par ??        \{\par ??        \}\par ??    \}\par ??}
--><div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px"><span style="COLOR: blue">public</span><span style="COLOR: blue">class</span><span style="COLOR: #2b91af">Url</span></p><p style="MARGIN: 0px">
{
</p><p style="MARGIN: 0px">
    [XmlAttribute(<span style="COLOR: #a31515">"address"</span>)]
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span><span style="COLOR: blue">string</span> Address;
</p><p style="MARGIN: 0px">
    [XmlAttribute(<span style="COLOR: #a31515">"primary"</span>), <span style="COLOR: #2b91af">DefaultValue</span>(<span style="COLOR: blue">false</span>)]
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span><span style="COLOR: blue">bool</span> Primary;
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span> Url()
</p><p style="MARGIN: 0px">
    {
</p><p style="MARGIN: 0px">
   }
</p><p style="MARGIN: 0px">
}
</p></div><!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20         \cf3 static\cf0  \cf3 void\cf0  Main(\cf3 string\cf0 [] args)\par ??        \{\par ??            \cf4 Url\cf0  u = \cf3 new\cf0  \cf4 Url\cf0 ();\par ??            u.Address = \cf5 "http://testing/"\cf0 ;\par ??            \cf3 string\cf0  xml = GetXMLFromObject(u);\par ??            \cf4 Console\cf0 .WriteLine(xml);\par ??            \cf4 Console\cf0 .ReadLine();\par ??        \}\par ??}
--><div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px"><span style="COLOR: blue">static</span><span style="COLOR: blue">void</span> Main(<span style="COLOR: blue">string</span>[]
args)
</p><p style="MARGIN: 0px">
{
</p><p style="MARGIN: 0px">
    <span style="COLOR: #2b91af">Url</span> u = <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">Url</span>();
</p><p style="MARGIN: 0px">
    u.Address = "<span style="COLOR: #a31515"><a href="http://testing/">http://testing/</a></span>";
</p><p style="MARGIN: 0px">
    <span style="COLOR: blue">string</span> xml = GetXMLFromObject(u);
</p><p style="MARGIN: 0px">
    <span style="COLOR: #2b91af">Console</span>.WriteLine(xml);
</p><p style="MARGIN: 0px">
}
</p></div></code></pre><p>
This produces roughly the following output (XML namespace information is stripped
here. I will show how to do this in code with a future post). Notice that the Primary
attribute is not generated because it matches the DefaultValue.<br /><code><pre><div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px">
&lt;?xml version=<span style="COLOR: #a31515">"1.0"</span> encoding=<span style="COLOR: #a31515">"utf-16"</span>?&gt;
</p><p style="MARGIN: 0px">
&lt;<span style="COLOR: #2b91af">Url</span> address=<span style="COLOR: #a31515">"http://testing/"</span> /&gt;
</p></div></pre></code><img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=6bb6b064-6d03-4485-949c-04e679e51f5f" /></p></body>
      <title>Marking Default Values to Control XML serialization</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,6bb6b064-6d03-4485-949c-04e679e51f5f.aspx</guid>
      <link>http://www.distribucon.com/blog/MarkingDefaultValuesToControlXMLSerialization.aspx</link>
      <pubDate>Mon, 08 Jan 2007 15:09:43 GMT</pubDate>
      <description>While the &lt;a href="http://www.distribucon.com/blog/PermaLink,guid,35155273-aa80-4d03-a871-243b89a23f9b.aspx" traget="_new"&gt;XmlIgnore/Specified&lt;/a&gt; pattern
to control serialization is very useful, it could be overkill for simpler serialization
scenarios. 
&lt;p&gt;
When you create an object with a bool field, that field is initialized and defaulted
to the value of false. Under normal conditions, this value will get serialized every
time. However, what if we only care about capturing that value when the value is true?
It turns out that we can use &lt;a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx" target=_new&gt;DefaultValueAttribute&lt;/a&gt;.
The first thing that may stand out to you is that this attribute lives in the System.ComponentModel
namespace, and not in an XML namespace. The online help also mentions that this is
used for visual designers and code generators. XmlSerializer also uses this attribute
to decide whether or not to stream the value during serialization, since it's really
a form of a code generator (more on that in a later post). If the value of the field
or property matches the value that you specify in the DefaultValueAttribute, then
that property will not be streamed. 
&lt;p&gt;
Using an &lt;a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx" target=_new&gt;XmlAttributeAttribute&lt;/a&gt;,
you can specify that a certain property will be expressed as an XML attribute instead
of an XML element. To demonstrate both of these concepts, see the following code:&lt;br&gt;
&lt;pre&gt;&lt;code&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20     \cf3 public\cf0  \cf3 class\cf0  \cf4 Url\par ??\cf0     \{\par ??        [XmlAttribute(\cf5 "address"\cf0 )]\par ??        \cf3 public\cf0  \cf3 string\cf0  Address;\par ??\par ??        [XmlAttribute(\cf5 "primary"\cf0 ), \cf4 DefaultValue\cf0 (\cf3 false\cf0 )]\par ??        \cf3 public\cf0  \cf3 bool\cf0  Primary;\par ??\par ??        \cf3 public\cf0  Url()\par ??        \{\par ??        \}\par ??    \}\par ??}
--&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Url&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlAttribute(&lt;span style="COLOR: #a31515"&gt;"address"&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Address;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlAttribute(&lt;span style="COLOR: #a31515"&gt;"primary"&lt;/span&gt;), &lt;span style="COLOR: #2b91af"&gt;DefaultValue&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;false&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Primary;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Url()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20         \cf3 static\cf0  \cf3 void\cf0  Main(\cf3 string\cf0 [] args)\par ??        \{\par ??            \cf4 Url\cf0  u = \cf3 new\cf0  \cf4 Url\cf0 ();\par ??            u.Address = \cf5 "http://testing/"\cf0 ;\par ??            \cf3 string\cf0  xml = GetXMLFromObject(u);\par ??            \cf4 Console\cf0 .WriteLine(xml);\par ??            \cf4 Console\cf0 .ReadLine();\par ??        \}\par ??}
--&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[]
args)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;Url&lt;/span&gt; u = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Url&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; u.Address = "&lt;span style="COLOR: #a31515"&gt;&lt;a href="http://testing/"&gt;http://testing/&lt;/a&gt;&lt;/span&gt;";
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; xml = GetXMLFromObject(u);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(xml);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
This produces roughly the following output (XML namespace information is stripped
here. I will show how to do this in code with a future post). Notice that the Primary
attribute is not generated because it matches the DefaultValue.&lt;br&gt;
&lt;code&gt;&lt;pre&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;lt;?xml version=&lt;span style="COLOR: #a31515"&gt;"1.0"&lt;/span&gt; encoding=&lt;span style="COLOR: #a31515"&gt;"utf-16"&lt;/span&gt;?&amp;gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Url&lt;/span&gt; address=&lt;span style="COLOR: #a31515"&gt;"http://testing/"&lt;/span&gt; /&amp;gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/code&gt;&gt;&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=6bb6b064-6d03-4485-949c-04e679e51f5f" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,6bb6b064-6d03-4485-949c-04e679e51f5f.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=35155273-aa80-4d03-a871-243b89a23f9b</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,35155273-aa80-4d03-a871-243b89a23f9b.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,35155273-aa80-4d03-a871-243b89a23f9b.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=35155273-aa80-4d03-a871-243b89a23f9b</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">David Hervieux posted a link to some <a href="http://www.devolutions.net/articles/serialization.aspx" target="_new">Serialization
FAQs</a> in a comment to my last post. This material is good, and actually forced
me to go back through what I wanted to cover, since I didn't want to just add posts
that duplicate what can be found easily on the web. I'll still move forward with a
few items in XML serialization that I think are unique. 
<p>
The <a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlignoreattribute.aspx" target="_new">XMLIgnore
attribute</a> is used to tell the XmlSerializer that this field is transient, and
should not be (de)serialized. The most typical use for this would be to mark calculated
fields with this attribute. This is an easy customization that helps your serialization
process, but it leaves a bit to be desired. It's an "all or nothing" approach, where
the property is either streamed or not. 
</p><p>
This <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.asp" target="_new">MSDN
article</a> hints at what to do to get more fine-grained control of this aspect of
serialization. Basically, you create a public boolean field or property named <i>propertyName</i>Specified
for the property you want to control. The XmlSerializer uses this convention to determine
whether or not to include the associated property for serialization. Since this new
Specified field will be eligible for serialization, be sure to mark it with the XmlIgnore
attribute to prevent it from being serialized. 
</p><p>
A very simple code listing showing how this all fits together:<br /></p><pre><code><div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"><p style="MARGIN: 0px">
    <span style="COLOR: blue">public</span><span style="COLOR: blue">class</span><span style="COLOR: #2b91af">Player</span></p><p style="MARGIN: 0px">
    {
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">private</span><span style="COLOR: blue">int</span> number;
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">private</span><span style="COLOR: blue">string</span> name;
</p><p style="MARGIN: 0px">
        [XmlElement(<span style="COLOR: #a31515">"jerseyNumber"</span>)]
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">public</span><span style="COLOR: blue">int</span> Number
</p><p style="MARGIN: 0px">
        {
</p><p style="MARGIN: 0px">
            <span style="COLOR: blue">get</span> { <span style="COLOR: blue">return</span> number;
}
</p><p style="MARGIN: 0px">
            <span style="COLOR: green">//
You could add more logic here to only set NumberSpecified based on some criteria</span></p><p style="MARGIN: 0px">
            <span style="COLOR: blue">set</span> {
number = <span style="COLOR: blue">value</span>; NumberSpecified = <span style="COLOR: blue">true</span>;
}
</p><p style="MARGIN: 0px">
        }
</p><p style="MARGIN: 0px">
        [XmlElement(<span style="COLOR: #a31515">"playerName"</span>)]
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">public</span><span style="COLOR: blue">string</span> Name
</p><p style="MARGIN: 0px">
        {
</p><p style="MARGIN: 0px">
            <span style="COLOR: blue">get</span> { <span style="COLOR: blue">return</span> name;
}
</p><p style="MARGIN: 0px">
            <span style="COLOR: blue">set</span> {
name = <span style="COLOR: blue">value</span>; }
</p><p style="MARGIN: 0px">
        }
</p><p style="MARGIN: 0px"></p><p style="MARGIN: 0px">
        [<span style="COLOR: #2b91af">XmlIgnore</span>]
</p><p style="MARGIN: 0px">
        <span style="COLOR: blue">public</span><span style="COLOR: blue">bool</span> NumberSpecified;
</p><p style="MARGIN: 0px">
    }
</p></div></code>The downsides to using this techniques are: 
</pre><ul></ul><li>
It requires another field, and some custom logic to make this work. 
</li><li>
It requires that the Specified field be marked as public, exposing it to consumers
of the library. There is no real reason why the XmlSerializer couldn't use reflection
to get at a non-public member. 
</li><li>
If you use a property, it must be a full read-write property. A read-only property
could very well suffice here, and make your code easier to maintain. 
<ul></ul><p>
Download the very simple sample showing this behavior <a href="http://www.distribucon.com/download/dotnet/ConditionalIgnore.zip" target="_new">here</a>.
</p></li><img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=35155273-aa80-4d03-a871-243b89a23f9b" /></body>
      <title>Ignoring specified elements during XMLSerialization</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,35155273-aa80-4d03-a871-243b89a23f9b.aspx</guid>
      <link>http://www.distribucon.com/blog/IgnoringSpecifiedElementsDuringXMLSerialization.aspx</link>
      <pubDate>Fri, 05 Jan 2007 19:37:20 GMT</pubDate>
      <description>David Hervieux posted a link to some &lt;a href="http://www.devolutions.net/articles/serialization.aspx" target=_new&gt;Serialization
FAQs&lt;/a&gt; in a comment to my last post. This material is good, and actually forced
me to go back through what I wanted to cover, since I didn't want to just add posts
that duplicate what can be found easily on the web. I'll still move forward with a
few items in XML serialization that I think are unique. 
&lt;p&gt;
The &lt;a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlignoreattribute.aspx" target=_new&gt;XMLIgnore
attribute&lt;/a&gt; is used to tell the XmlSerializer that this field is transient, and
should not be (de)serialized. The most typical use for this would be to mark calculated
fields with this attribute. This is an easy customization that helps your serialization
process, but it leaves a bit to be desired. It's an "all or nothing" approach, where
the property is either streamed or not. 
&lt;p&gt;
This &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.asp" target=_new&gt;MSDN
article&lt;/a&gt; hints at what to do to get more fine-grained control of this aspect of
serialization. Basically, you create a public boolean field or property named &lt;i&gt;propertyName&lt;/i&gt;Specified
for the property you want to control. The XmlSerializer uses this convention to determine
whether or not to include the associated property for serialization. Since this new
Specified field will be eligible for serialization, be sure to mark it with the XmlIgnore
attribute to prevent it from being serialized. 
&lt;p&gt;
A very simple code listing showing how this all fits together:&lt;br&gt;
&lt;pre&gt;&lt;code&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Player&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; number;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlElement(&lt;span style="COLOR: #a31515"&gt;"jerseyNumber"&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; Number
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; number;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;//
You could add more logic here to only set NumberSpecified based on some criteria&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; {
number = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; NumberSpecified = &lt;span style="COLOR: blue"&gt;true&lt;/span&gt;;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; [XmlElement(&lt;span style="COLOR: #a31515"&gt;"playerName"&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Name
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; name;
}
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; {
name = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="COLOR: #2b91af"&gt;XmlIgnore&lt;/span&gt;]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; NumberSpecified;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;/code&gt;The downsides to using this techniques are: &gt;
&lt;/pre&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;li&gt;
It requires another field, and some custom logic to make this work. 
&lt;li&gt;
It requires that the Specified field be marked as public, exposing it to consumers
of the library. There is no real reason why the XmlSerializer couldn't use reflection
to get at a non-public member. 
&lt;li&gt;
If you use a property, it must be a full read-write property. A read-only property
could very well suffice here, and make your code easier to maintain. 
&lt;ul&gt;
&lt;/ul&gt;
&lt;p&gt;
Download the very simple sample showing this behavior &lt;a href="http://www.distribucon.com/download/dotnet/ConditionalIgnore.zip" target=_new&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;/li&gt;&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=35155273-aa80-4d03-a871-243b89a23f9b" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,35155273-aa80-4d03-a871-243b89a23f9b.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.distribucon.com/blog/Trackback.aspx?guid=dac80ab7-9376-47f3-88c1-87c2c9778434</trackback:ping>
      <pingback:server>http://www.distribucon.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.distribucon.com/blog/PermaLink,guid,dac80ab7-9376-47f3-88c1-87c2c9778434.aspx</pingback:target>
      <dc:creator>Dan Miser</dc:creator>
      <wfw:comment>http://www.distribucon.com/blog/CommentView,guid,dac80ab7-9376-47f3-88c1-87c2c9778434.aspx</wfw:comment>
      <wfw:commentRss>http://www.distribucon.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=dac80ab7-9376-47f3-88c1-87c2c9778434</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Introduction to XmlSerializer</title>
      <guid isPermaLink="false">http://www.distribucon.com/blog/PermaLink,guid,dac80ab7-9376-47f3-88c1-87c2c9778434.aspx</guid>
      <link>http://www.distribucon.com/blog/IntroductionToXmlSerializer.aspx</link>
      <pubDate>Wed, 27 Dec 2006 17:05:11 GMT</pubDate>
      <description>Serializing between .NET objects and XML files, and vice versa, is extremely easy, yet flexible and powerful. I'll be blogging a few entries about this topic in the days to come. This entry will set the stage, showing the basics of XML serialization. 
&lt;p&gt;
Assume you have a (stripped down) class definition like this:&lt;br&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20     \cf2 public\cf0  \cf2 class\cf0  \cf10 Player\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 int\cf0  Number \{ \cf2 get\cf0 ; \cf2 set\cf0 ; \}\par ??        \cf2 public\cf0  \cf2 string\cf0  Name \{ \cf2 get\cf0 ; \cf2 set\cf0 ; \} \par ??    \}\par ??}
--&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: teal"&gt;Player&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[XmlElement("jerseyNumber")]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; Number
{ &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[XmlElement("playerName")]
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Name
{ &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; } 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
And you wanted to read/write an XML file that looked like this:&lt;br&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 &amp;lt;Player&amp;gt;\par ??  &amp;lt;jerseyNumber&amp;gt;23&amp;lt;/jerseyNumber&amp;gt;\par ??  &amp;lt;playerName&amp;gt;Dan&amp;lt;/playerName&amp;gt;\par ??&amp;lt;/Player&amp;gt;\par ??}
--&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;lt;Player&amp;gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp; &amp;lt;jerseyNumber&amp;gt;23&amp;lt;/jerseyNumber&amp;gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp; &amp;lt;playerName&amp;gt;Dan&amp;lt;/playerName&amp;gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;lt;/Player&amp;gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The key to making this happen is the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx" target=_new&gt;XmlSerializer
class&lt;/a&gt;. This is the class that will convert between public properties and XML elements.
You can also decorate your class with simple attributes to coerce your class into
generating custom XML (i.e. XML attributes, hide or rename elements, etc.). 
&lt;p&gt;
Here are a couple of helper methods, and a sample that uses them to show you how all
of this comes together. 
&lt;p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20         \cf2 static\cf0  \cf2 string\cf0  GetXMLFromObject(\cf2 object\cf0  obj)\par ??        \{\par ??            \cf10 XmlSerializer\cf0  ser = \cf2 new\cf0  \cf10 XmlSerializer\cf0 (obj.GetType());\par ??\par ??            \cf10 StringBuilder\cf0  sb = \cf2 new\cf0  \cf10 StringBuilder\cf0 ();\par ??            \cf2 using\cf0  (\cf10 XmlTextWriter\cf0  writer = \cf2 new\cf0  \cf10 XmlTextWriter\cf0 (\cf2 new\cf0  \cf10 StringWriter\cf0 (sb)))\par ??            \{\par ??                writer.Formatting = \cf10 Formatting\cf0 .Indented;\par ??                ser.Serialize(writer, obj);\par ??                \cf2 return\cf0  sb.ToString();\par ??            \}\par ??        \}\par ??\par ??        \cf2 static\cf0  \cf2 object\cf0  CreateObjectFromXML(\cf2 string\cf0  xml, \cf10 Type\cf0  xmlType)\par ??        \{\par ??            \cf11 // By specifying XmlRootAttribute, we can reconstitute the object without a namespace in the xml file\par ??\cf0             \cf10 XmlSerializer\cf0  ser = \cf2 new\cf0  \cf10 XmlSerializer\cf0 (xmlType);\par ??            \cf2 object\cf0  obj = \cf2 null\cf0 ;\par ??            \cf2 using\cf0  (\cf10 StringReader\cf0  reader = \cf2 new\cf0  \cf10 StringReader\cf0 (xml))\par ??            \{\par ??                obj = ser.Deserialize(reader);\par ??            \}\par ??            \cf2 return\cf0  obj;\par ??        \}\par ??        \par ??        \cf2 static\cf0  \cf2 void\cf0  Main(\cf2 string\cf0 [] args)\par ??        \{\par ??            \cf10 Player\cf0  p = \cf2 new\cf0  \cf10 Player\cf0 ();\par ??            p.Number = 23;\par ??            p.Name = \cf13 "Dan"\cf0 ;\par ??            \cf2 string\cf0  xml = GetXMLFromObject(p);\par ??            \cf10 Console\cf0 .WriteLine(xml);\par ??            \cf10 Console\cf0 .WriteLine();\par ??\par ??            \cf10 Player\cf0  q = (\cf10 Player\cf0 )CreateObjectFromXML(xml, \cf2 typeof\cf0 (\cf10 Player\cf0 ));\par ??            \cf10 Console\cf0 .WriteLine(q.Number + \cf13 " --&amp;gt; "\cf0  + q.Name);\par ??            \cf10 Console\cf0 .ReadLine();\par ??        \}\par ??}
--&gt;
&lt;div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; GetXMLFromObject(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; obj)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;XmlSerializer&lt;/span&gt; ser
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;XmlSerializer&lt;/span&gt;(obj.GetType());
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;StringBuilder&lt;/span&gt; sb
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;StringBuilder&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: teal"&gt;XmlTextWriter&lt;/span&gt; writer
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;XmlTextWriter&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;StringWriter&lt;/span&gt;(sb)))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Formatting
= &lt;span style="COLOR: teal"&gt;Formatting&lt;/span&gt;.Indented;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ser.Serialize(writer,
obj);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; sb.ToString();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;object&lt;/span&gt; CreateObjectFromXML(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; xml, &lt;span style="COLOR: teal"&gt;Type&lt;/span&gt; xmlType)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;XmlSerializer&lt;/span&gt; ser
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;XmlSerializer&lt;/span&gt;(xmlType);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;object&lt;/span&gt; obj
= &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: teal"&gt;StringReader&lt;/span&gt; reader
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;StringReader&lt;/span&gt;(xml))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; obj =
ser.Deserialize(reader);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; obj;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[]
args)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;Player&lt;/span&gt; p
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;Player&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; p.Number = 23;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; p.Name = &lt;span style="COLOR: maroon"&gt;"Dan"&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; xml
= GetXMLFromObject(p);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;.WriteLine(xml);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;.WriteLine();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;Player&lt;/span&gt; q
= (&lt;span style="COLOR: teal"&gt;Player&lt;/span&gt;)CreateObjectFromXML(xml, &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: teal"&gt;Player&lt;/span&gt;));
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;.WriteLine(q.Number
+ &lt;span style="COLOR: maroon"&gt;" --&amp;gt; "&lt;/span&gt; + q.Name);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;.ReadLine();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Other references:&lt;br&gt;
&lt;a href="http://msdn2.microsoft.com/en-us/library/ms950721.aspx" target=_new&gt;XML Serialization
in the .NET Framework&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.distribucon.com/blog/aggbug.ashx?id=dac80ab7-9376-47f3-88c1-87c2c9778434" /&gt;</description>
      <comments>http://www.distribucon.com/blog/CommentView,guid,dac80ab7-9376-47f3-88c1-87c2c9778434.aspx</comments>
      <category>.NET</category>
      <category>XML</category>
    </item>
  </channel>
</rss>