xml格式
<Person>
<Student StuID="11">
<StuName>张三</StuName>
</Student >
<Student StuID="22">
<StuName>历史</StuName>
</Student >
</Person>
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\temp\a.xml");
XmlNodeList students = doc.DocumentElement.ChildNodes; //Student节点集合
foreach (XmlNode stu in students)
{
XmlElement element = (XmlElement)stu;
string stuId = element.GetAttribute("StuID");
XmlNode nameNode = element.SelectSingleNode("StuName");
string name = nameNode.InnerText;
Console.WriteLine(stuId+","+name);
}
生成xml文档
public static void Test()
{
XmlDocument doc = new XmlDocument();
XmlElement ePersons = doc.CreateElement("Persons");
doc.AppendChild(ePersons);
foreach (Person person in ePersons)
{
XmlElement epPerson = doc.CreateElement("Person");
epPerson.SetAttribute("id",person.Age.ToString());
XmlElement eName = doc.CreateElement("Name");
eName.InnerText = person.Name;
XmlElement eAge = doc.CreateElement("Age");
eAge.InnerText = person.Age.ToString();
epPerson.AppendChild(eName);
epPerson.AppendChild(eAge);
ePersons.AppendChild(epPerson);
}
doc.Save("c:/temp/1.xml");
}