【问题标题】:C# XML Serialization/DeserializationC# XML 序列化/反序列化
【发布时间】:2011-05-12 09:09:30
【问题描述】:

我是 C# 的新手。我现在正在上课,我们的一个课程示例无法编译。 Visual Studio 2010 给我这个错误:XML 文档中存在错误 (3, 2)。

我应该如何编辑 XML 文件以使其与代码一起使用?

感谢您的帮助!

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

使用系统; 命名空间 XmlArraySerialize { /// /// XmlArraySerialize 示例:序列化和反序列化 /// 一个人的数组。 ///

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;

    }
}

}

还有 XML 文件...

<?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>
        <Gender>F</Gender>
        <Age>30</Age>
    </Person>
</Persons>

【问题讨论】:

  • 你能把“Person”类的代码也贴出来吗?此外,您提到它是编译错误,但我认为VS不会打扰您的XML文件,您将XML文件放在哪里?它不应该包含在 VS 项目中(至少不需要编译)。
  • 好的,已编辑以包含 Person 类。 XML 文件就在 /bin/debug 文件夹中。
  • stackoverflow.com/questions/10109608/…这个帖子有解决办法。

标签: c# .net xml serialization deserialization


【解决方案1】:

这应该有效)

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

【讨论】:

  • 您好,感谢您的回复!我想知道是否有一种方法可以简单地编辑 XML 文件以便正确加载?
  • @Taj G,我没有看到任何简单的方法来编辑 XML 以使其加载。您的代码没有引用任何名为 Persons 的内容,这是您遇到运行时错误的地方。之后,您的代码在 XML 文件中没有任何 ID 属性。您需要更改一些代码才能使其正常工作。
【解决方案2】:

感谢您的意见!我已经解决了这个问题。由于代码中没有 main 方法,我必须编辑属性,以便 SerializeOut 在 SerializeIn 之前工作。我猜 Persons XML 文件只是一个模板...再次感谢!

【讨论】:

    猜你喜欢
    • 2012-02-02
    • 2010-11-25
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多