[XmlRoot(Namespace = "http://mycompany.com")]
public class StudentXmlData
{
[XmlElement]
public string id { get; set; }
[XmlElement]
public string name { get; set; }
[XmlElement]
public int age { get; set; }
}
static void SaveXmlData()
{
StudentXmlData sd = new StudentXmlData();
sd.id = "001";
sd.name = "西门吹雪";
sd.age = 34;
System.Xml.Serialization.XmlSerializer xmlFormatter = new System.Xml.Serialization.XmlSerializer(typeof(StudentXmlData));
using (Stream stream = new FileStream("dataxml.xml", FileMode.Create, FileAccess.Write, FileShare.None))
{
xmlFormatter.Serialize(stream, sd);
}
}
结果:
<?xml version="1.0"?>
<StudentXmlData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://mycompany.com">
<id>001</id>
<name>西门吹雪</name>
<age>34</age>
</StudentXmlData>