开发者

C# XML Serialization/Deserialization

开发者 https://www.devze.com 2023-01-26 08:18 出处:网络
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).

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).

How should I edit the XML file to make it work with the code?

Thank you for your help!

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();
    }
}

using System; namespace XmlArraySerialize { /// /// XmlArraySerialize Example: Serializes and deserializes /// an array of Person. ///

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;

    }
}

}

And the XML file...

<?xml version="1.0" standalone="no"?>
<!--Created by ToXml Example in IO-->
<Persons>
    <Person ID="1001">
        <Name>Susan</Name>
        <Gender>F</Gender>
        <Age>21</Age>
    </Person>
    <Person ID="1002">
        <Name>Michael</Name>
        <Gender>M</Gender>
        <Age>25</Age>
    </Person>
    <Person ID="1003">
        <Name>Judy</Name>
        <Gender>F</Gender>
        <Age>31</Age>
    </Person>
    <Person ID="1004">
        <Name>Chloe</Name>
        <Gender>F</Gender>
        <Age>27</Age>
    </Person>
    <Person ID="1005">
        <Name>Scott</Name>
        <Gender>M</Gender>
        <Age>58</Age>
    </Person>
    <Person ID="1006">
        <Name>William</Name>
        <Gender>M</Gender>
        <Age>41</Age>
    </Person>
    <Person ID="1007">
        <Name>Mary</Name>
        &开发者_如何学Clt;Gender>F</Gender>
        <Age>30</Age>
    </Person>
</Persons>


This should work )

class Program
{
    static void Main(string[] args)
    {   
        const string infile = "x:\\Persons.xml";
        Persons p;

        using (var sr = new StreamReader(infile))
        {
            var xs = new XmlSerializer(p.GetType());
            p = (Persons)(xs.Deserialize(sr));
        }

        Console.WriteLine("Deserialized array p from output file " + infile + ".");

        // Print array.
        foreach (var x in p)
            Console.WriteLine(x);

        Console.ReadLine();
    }
}

[XmlType(TypeName = "Persons")]
public class Persons : IEnumerable<Person>
{
    private List<Person> inner = new List<Person>();

    public void Add(object o)
    {
        inner.Add((Person)o);
    }

    public IEnumerator<Person> GetEnumerator()
    {
        return inner.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}


public class Person
{
    [XmlAttribute]
    public int ID { get; set; }

    public string Name { get; set; }
    public string Gender { get; set; }
    public int Age { get; set; }
}

More about XmlType, More about XmlAttribute


Thank you for your input! I have solved the problem. Since there is no main method in the code, I had to edit the properties so that SerializeOut works before SerializeIn. I guess the Persons XML file is just a template... Thanks again!

0

精彩评论

暂无评论...
验证码 换一张
取 消