1.右击解决方案->添加->新建项->XML文档
2.下面就是一个简单的XML文档,名为"Student":
3.读取XML文档
XmlDocument myXml = new XmlDocument();
myXml.Load("Student.xml");//读取指定的XML文档
XmlNode student = myXml.DocumentElement;//读取XML的根节点
foreach(XmlNode node in student.ChildNodes)//对子节点进行循环
{
//将每个节点的内容显示出来
switch(node.Name)
{
case"Name"
Console.WriteLine("姓名:{0}",node.InnerText);
break;
case"Age"
Console.WriteLine("年龄:{0}",node.InnerText);
break;
case"Hobby"
Console.WriteLine("爱好:{0}",node.InnerText);
break;
}
}
希望对你有所帮助。
using System.Xml;
//写
XmlDocument doc = new XmlDocument();
doc.InnerXml = "内容";
doc.Save(@"C:\x.xml");
//读
XmlDocument doc = new XmlDocument();
doc.Load("@"C:\x.xml"");
//XML文档
//读取
XmlTextReader textReader = new XmlTextReader("sz.xml");
textReader.Read();
int i = 0;
string dq="";
string dq1="";
string dq2="";
string dq3="";
string dq4="";
string dq5="";
while (textReader.Read())
{
textReader.MoveToElement();
if (textReader.NodeType == XmlNodeType.Text)
{
switch (i)
{
case 0: dq = textReader.Value; break;
case 1: dq1 = textReader.Value; break;
case 2: dq2 = textReader.Value; break;
case 3: dq3 = textReader.Value; break;
case 4: dq4 = textReader.Value; break;
case 5: dq5 = textReader.Value; break;
}
i++;
}
}
//写
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("sz.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("sz").ChildNodes;
foreach (XmlNode xn1 in nodeList)
{
XmlElement xe2 = (XmlElement)xn1;
if (xe2.Name == "yrzkf")
{
xe2.InnerText = yrzkf.ToString();// ToString前的是你要保存的内容
}
if (xe2.Name == "rzzrs")
{
xe2.InnerText = rzzrs.ToString();
}
if (xe2.Name == "wqjkf")
{
xe2.InnerText = wqjkf.ToString();
}
if (xe2.Name == "je")
{
xe2.InnerText = je.ToString();
}
}
xmlDoc.Save("sz.xml");
写xml
1.把数据装到dataset里,
2.dataset.writexml("F:\\aaa.xml",XmlWriteMode.WriteSchema);
读xml
DataSet ds = new DataSet();
ds.ReadXml(("F:\\aaa.xml");
完了,就这么简单.