Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp needed in XML Serialization
    text
    copied!<p>I am trying a write a program to serialize a object to a xml file.</p> <pre><code> [XmlRoot ("Person")] public class person { [XmlElement("Name")] public string name { get; set; } [XmlElement("Age")] public int age { get; set; } [XmlElement ("Location")] location _location = new location { city = "Delhi", country = "India", distance = 123 }; } </code></pre> <p>This is the class which object I want to serialize.</p> <p>The code I am using to serialze is</p> <pre><code> person _person = new person { name = "ASDF", age = 25}; System.Xml.Serialization.XmlSerializer XS = new System.Xml.Serialization.XmlSerializer(typeof(person)); System.IO.TextWriter TW = new System.IO.StreamWriter(System.IO.File.Create("C:\\Users\\vaibhav.1.jain\\Documents\\Visual Studio 2010\\Projects\\LinqWeb\\LinqWeb\\xmlser\\ser4.xml")); XS.Serialize(TW, _person); TW.Close(); </code></pre> <p>And the XML I am getting is</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Name&gt;ASDF&lt;/Name&gt; &lt;Age&gt;25&lt;/Age&gt; &lt;/Person&gt; </code></pre> <p>But I should have got</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Name&gt;ASDF&lt;/Name&gt; &lt;Age&gt;25&lt;/Age&gt; &lt;location&gt; &lt;country&gt;India&lt;/country&gt; &lt;city&gt;Delhi&lt;/city&gt; &lt;distance&gt;12&lt;/distance&gt; &lt;/location&gt; &lt;/Person&gt; </code></pre> <p>Can you tell me what I am doing wrong, I am new to XML and serialization.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload