创建xml
static void Main(string[] args)
{ //1.创建一个XDocument对象
XDocument writeXml = new XDocument();
//2.增加一个根节点
XElement XRoot = new XElement("WebSites");
//3.将根节点加到writeXML中
writeXml.Add(XRoot);
//4.写子节点
XElement XeBaidu = new XElement("WebSite");
XeBaidu.SetAttributeValue("url", "http://www.baidu.com");
//5.把子节点加到根节点上
//快速添加子节点
XRoot.SetElementValue("webSiteCount",100);
XRoot.Add(XeBaidu);
//继续增加子元素增加子标签
XeBaidu.SetElementValue("name", "笑笑");
XeBaidu.SetElementValue("age", "19");
writeXml.Save("Web.xml");
Console.WriteLine("OK");
Console.ReadKey();
}
解析XML
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"E:\practice\XML操作\订单练习\orders.xml");
XElement orderElement = doc.Root;
XElement customerElement= orderElement.Element("CustomerName");
XElement orderNumberElement = orderElement.Element("orderNumber");
XElement orderItems = orderElement.Element("items");
List<string> orderName = new List<string>();
List<string> orderCount = new List<string>();
foreach (XElement order in orderItems.Elements())
{
orderName.Add(order.Attribute("Name").Value);
orderCount.Add(order.Attribute("Count").Value);
}
Console.WriteLine(customerElement.Value+"的订单"+orderNumberElement.Value+"买");
Console.WriteLine(orderNae[0] +orderCount[0]+orderName[1]+orderCount[1]+orderName[2]+orderCount[2]);
Console.ReadKey();
}
////////////////XML 文件
<?xml version="1.0" encoding="utf-8" ?>
<order>
<CustomerName>小肖</CustomerName>
<orderNumber>BJ200888</orderNumber>
<items>
<orederItem Name="电脑" Count="30"></orederItem>
<orederItem Name="电视" Count="10"></orederItem>
<orederItem Name="水杯" Count="20"></orederItem>
</items>
</order>
XML序列化
static void Main(string[] args)
{
Person per = new Person();
per.name = "小明";
per.age = 18;
Person per1 = new Person();
per1.name = "dfafd";
per1.age = 12;
List<Person> list = new List<Person>();
list.Add(per);
list.Add(per1);
//XmlSerializer xmlSer = new XmlSerializer(typeof(Person));
//using (FileStream fs = File.OpenWrite("Person.xml"))
//{
// xmlSer.Serialize(fs, per);
//}
XmlSerializer xmlser=new XmlSerializer(typeof(List<Person>));
using (FileStream fs = File.OpenWrite("list.xml")) {
xmlser.Serialize(fs, list);
}
Console.WriteLine("OK");
Console.ReadKey();
}
}
public class Person {
public string name { get; set; }
public int age { get; set; }
}
studentmessage.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<student>
<net09>
<preson id="cz001">
<name>小小</name>
<age>22</age>
<sex>女</sex>
</preson>
<person id="cz002">
<name>小周</name>
<age>21</age>
<sex>男</sex>
</person>
</net09>
<net08>
<person id="cx001">
<name>嘉怡</name>
<age>16</age>
<sex>女</sex>
</person>
<person id="cx002">
<name>小肖</name>
<age>18</age>
<sex>男</sex>
</person>
</net08>
</student>
XML加载
private void btnXml_Click(object sender, EventArgs e)
{
//1加载
XDocument docxml = XDocument.Load(@"E:\practice\XML操作\XML操作\studentmessage.xml");
//2.获取根节点
XElement xmlRoot = docxml.Root;
//3.把根节点加到TreeView上
TreeNode treeviewroot = tvXmlShow.Nodes.Add(xmlRoot.Name.ToString());
//4.递归加载
LoadNodes(xmlRoot,treeviewroot);
}
//foreach没了,就停止了
private void LoadNodes(XElement xmlRoot, TreeNode treeviewroot)
{
foreach (XElement ele in xmlRoot.Elements())
{
if (ele.Elements().Count() > 0)
{ //绑定的是元素本身
TreeNode node = treeviewroot.Nodes.Add(ele.Name.ToString());
//如果有属性的话,怎遍历属性
XAttribute eleAttribute= ele.Attribute("id");
if (eleAttribute != null) {
node.Text += "{" + eleAttribute.Name + "=" + eleAttribute.Value + "}";
}
LoadNodes(ele, node);
}
else { TreeNode node = treeviewroot.Nodes.Add(ele.Value); }
}
}

经典案例银行xml
ytbank.xml
<?xml version="1.0" encoding="utf-8" ?>
<CFX>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000001"/>
<金额 val="1234567890.12"/>
<付款机构 val="腾讯销售部"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="新浪财务部"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000002"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000003"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000004"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000005"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000006"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000007"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000008"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000009"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000010"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000011"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000012"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000013"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000014"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
<MSG>
<交易码 val="1000"/>
<流水号 val="100000000000000015"/>
<金额 val="1234567890.12"/>
<付款机构 val="1234"/>
<付款单位账号 val="12345678901234567890"/>
<收款机构 val="1234"/>
<收款单位账号 val="12345678901234567890"/>
</MSG>
</CFX>
解析
static void Main(string[] args)
{
XDocument bankdoc = XDocument.Load(@"E:\practice\XML操作\银行bank解析\ytbank.xml");
XElement bankroot = bankdoc.Root;
string businesscode, serialnumber, money, payorganization, payunit, getorganization, getunit;
foreach (XElement bankmsg in bankroot.Elements()) {
businesscode = bankmsg.Element("交易码").Attribute("val").Value;
serialnumber = bankmsg.Element("流水号").Attribute("val").Value;
money = bankmsg.Element("金额").Attribute("val").Value;
payorganization = bankmsg.Element("付款机构").Attribute("val").Value;
payunit = bankmsg.Element("付款单位账号").Attribute("val").Value;
getorganization = bankmsg.Element("收款机构").Attribute("val").Value;
getunit = bankmsg.Element("收款单位账号").Attribute("val").Value;
Console.WriteLine("交易码" + businesscode + "流水号" + serialnumber + "金额" + money);
Console.WriteLine("付款机构" + payorganization + "付款单位账号" + payunit);
Console.WriteLine("收款机构" + getorganization + "收款单位账号 " + getunit);
Console.WriteLine();
}
Console.ReadKey();
}
订单练习
orders.xml
<?xml version="1.0" encoding="utf-8" ?>
<order>
<CustomerName>小肖</CustomerName>
<orderNumber>BJ200888</orderNumber>
<items>
<orederItem Name="电脑" Count="30"></orederItem>
<orederItem Name="电视" Count="10"></orederItem>
<orederItem Name="水杯" Count="20"></orederItem>
</items>
</order>
解析
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"E:\practice\XML操作\订单练习\orders.xml");
XElement orderElement = doc.Root;
XElement customerElement= orderElement.Element("CustomerName");
XElement orderNumberElement = orderElement.Element("orderNumber");
XElement orderItems = orderElement.Element("items");
List<string> orderName = new List<string>();
List<string> orderCount = new List<string>();
foreach (XElement order in orderItems.Elements())
{
orderName.Add(order.Attribute("Name").Value);
orderCount.Add(order.Attribute("Count").Value);
}
Console.WriteLine(customerElement.Value+"的订单"+orderNumberElement.Value+"买");
Console.WriteLine(orderName[0] + orderCount[0]+orderName[1]+orderCount[1]+orderName[2]+orderCount[2]);
Console.ReadKey();
}