Note that there are some explanatory texts on larger screens.

plurals
  1. POC# XML Serialization/Deserialization
    text
    copied!<p>I am brand new to C#. I'm taking a class on it right now, and one of our class examples wont compile. Visual Studio 2010 gives me this error: There is an error in XML document (3, 2).</p> <p>How should I edit the XML file to make it work with the code?</p> <p>Thank you for your help!</p> <pre><code>public class SerializeIn { public static void Main() { // Declarations. Person[] p = new Person[0]; string infile = "Persons.xml"; StreamReader sr = new StreamReader(infile); XmlSerializer xs = new XmlSerializer(p.GetType()); // Deserialize Person object from disc. p = (Person[])(xs.Deserialize(sr)); // Close StreamReader object to be safe. sr.Close(); // Write what happened. Console.WriteLine("Deserialized array p from output file " + infile + "."); // Print array. foreach(Person x in p) Console.WriteLine(x); Console.ReadLine(); } } </code></pre> <p>using System; namespace XmlArraySerialize { /// /// XmlArraySerialize Example: Serializes and deserializes /// an array of Person. /// </p> <pre><code>public class Person { public string name; public string gender; public int age; // Noarg constructor needed for compatibility public Person() { } public Person(string theName, string theGender, int theAge) { name = theName; gender = theGender; age = theAge; } public override string ToString() { return name + " " + gender + " " + age; } } </code></pre> <p>}</p> <p>And the XML file...</p> <pre><code>&lt;?xml version="1.0" standalone="no"?&gt; &lt;!--Created by ToXml Example in IO--&gt; &lt;Persons&gt; &lt;Person ID="1001"&gt; &lt;Name&gt;Susan&lt;/Name&gt; &lt;Gender&gt;F&lt;/Gender&gt; &lt;Age&gt;21&lt;/Age&gt; &lt;/Person&gt; &lt;Person ID="1002"&gt; &lt;Name&gt;Michael&lt;/Name&gt; &lt;Gender&gt;M&lt;/Gender&gt; &lt;Age&gt;25&lt;/Age&gt; &lt;/Person&gt; &lt;Person ID="1003"&gt; &lt;Name&gt;Judy&lt;/Name&gt; &lt;Gender&gt;F&lt;/Gender&gt; &lt;Age&gt;31&lt;/Age&gt; &lt;/Person&gt; &lt;Person ID="1004"&gt; &lt;Name&gt;Chloe&lt;/Name&gt; &lt;Gender&gt;F&lt;/Gender&gt; &lt;Age&gt;27&lt;/Age&gt; &lt;/Person&gt; &lt;Person ID="1005"&gt; &lt;Name&gt;Scott&lt;/Name&gt; &lt;Gender&gt;M&lt;/Gender&gt; &lt;Age&gt;58&lt;/Age&gt; &lt;/Person&gt; &lt;Person ID="1006"&gt; &lt;Name&gt;William&lt;/Name&gt; &lt;Gender&gt;M&lt;/Gender&gt; &lt;Age&gt;41&lt;/Age&gt; &lt;/Person&gt; &lt;Person ID="1007"&gt; &lt;Name&gt;Mary&lt;/Name&gt; &lt;Gender&gt;F&lt;/Gender&gt; &lt;Age&gt;30&lt;/Age&gt; &lt;/Person&gt; &lt;/Persons&gt; </code></pre>
 

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