C# XML 序列化/反序列化
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效)
有关 XmlType 的更多信息,< a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx" rel="nofollow">有关 XmlAttribute 的更多信息
This should work )
More about XmlType, More about XmlAttribute
感谢您的意见!我已经解决了这个问题。由于代码中没有 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!