C#XML序列化/反序列化 [英] C# XML Serialization/Deserialization
问题描述
我是全新的为C#。我是在一个类就可以了,现在,我们班的一个例子不会编译。 Visual Studio 2010中给了我这个错误:有一个在XML文档中的错误(3,2)
我应该如何编辑XML文件,以使其与code的工作?
感谢您的帮助!
公共类SerializeIn
{
公共静态无效的主要()
{
//声明。
人[] P =新的Person [0];
字符串INFILE =Persons.xml;
StreamReader的SR =新的StreamReader(INFILE);
XmlSerializer的XS =新的XmlSerializer(p.GetType());
//从磁盘反序列化Person对象。
P =(人[])(xs.Deserialize(SR));
//是安全的关闭StreamReader对象。
sr.Close();
//写了什么事。
Console.WriteLine(从输出文件反序列化阵列P+
INFILE +)。;
//打印阵列。
的foreach(人的X P)
Console.WriteLine(X);
到Console.ReadLine();
}
}
使用系统; 命名空间XmlArraySerialize { /// /// XmlArraySerialize例:序列化和反序列化 ///人的阵列。 ///
公共类Person
{
公共字符串名称;
公共字符串性别;
公众诠释年龄;
// NOARG构造所需的兼容性
公众人物(){}
公众人物(字符串theName,串theGender,诠释theAge)
{
NAME = theName;
性别= theGender;
年龄= theAge;
}
公共重写字符串的ToString()
{
返回名称++性别++年龄;
}
}
}
和XML文件...
< XML版本=1.0独立=无&GT?;
<! - toxml用于通过实例的IO创建 - >
<人>
<人员ID =1001>
<名称>&苏珊LT; /名称>
<性别> F< /性别>
<年龄> 21< /年龄>
< /人>
<人员ID =1002>
<名称>&迈克尔LT; /名称>
<性别> M< /性别>
<年龄> 25℃/年龄>
< /人>
<人员ID =1003>
<名称>&朱迪LT; /名称>
<性别> F< /性别>
<年龄> 31< /年龄>
< /人>
<人员ID =1004>
<名称>&克洛伊LT; /名称>
<性别> F< /性别>
<年龄> 27< /年龄>
< /人>
<人员ID =1005>
<名称>&斯科特LT; /名称>
<性别> M< /性别>
<年龄> 58< /年龄>
< /人>
<人员ID =1006>
<名称>威廉与LT; /名称>
<性别> M< /性别>
<年龄> 41< /年龄>
< /人>
<人员ID =1007>
<名称>&玛丽LT; /名称>
<性别> F< /性别>
<年龄> 30℃/年龄>
< /人>
< /人>
这应该工作)
类节目
{
静态无效的主要(字串[] args)
{
常量字符串INFILE =X:\\ Persons.xml;
人磷;
使用(VAR SR =新的StreamReader(INFILE))
{
VAR XS =新的XmlSerializer(p.GetType());
P =(人)(xs.Deserialize(SR));
}
Console.WriteLine(+ INFILE +,从输出文件的反序列化阵列P。);
//打印阵列。
的foreach(VAR的X P)
Console.WriteLine(X);
到Console.ReadLine();
}
}
[XmlType将(类型名=人)]
公共类人:IEnumerable的<人>
{
私人列表<人>内部=新的名单,其中,人物>();
公共无效添加(对象o)
{
inner.Add((人)O);
}
公众的IEnumerator<人>的GetEnumerator()
{
返回inner.GetEnumerator();
}
IEnumerator的IEnumerable.GetEnumerator()
{
返回的GetEnumerator();
}
}
公共类Person
{
[XmlAttribute]
公众诠释ID {获得;组; }
公共字符串名称{;组; }
公共字符串性别{获得;组; }
公众诠释年龄{获得;组; }
}
更多XmlType将,<一个HREF =http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx相对=nofollow>更多关于XmlAttribute
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>
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
这篇关于C#XML序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!