C# XML 序列化/反序列化

发布于 2024-10-03 23:40:50 字数 2732 浏览 0 评论 0原文

我是 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 示例:序列化和反序列化 /// 一个 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;

    }
}

}

以及 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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2024-10-10 23:40:50

这应该有效)

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

有关 XmlType 的更多信息,< a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx" rel="nofollow">有关 XmlAttribute 的更多信息

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

错々过的事 2024-10-10 23:40:50

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文