xml 可扩展的标记语言

-----------------20150123 xml 可扩展的标记语言--------------------------------------
xml 可扩展的标记语言
xml:存储数据
注意:
xml是严格区分大小写的。
xml标签也是成对出现的。
xml文档有且只有一个根节点。
节点
元素

<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<Name>金**</Name>
<Price>10</Price>
<Des>好看,不解释。</Des>
</Book>
</Books>

//通过代码来创建xml文档
//1、引用命名空间 using System.Xml
//2、创建xml文档对象
XmlDocument doc=new XmlDocument();
//3、创建第一个行描述信息,并且添加到doc文档中
XmlDeclaration dec=doc.CreateXmlDeclaration("1.0","utf-8",null);
doc.AppendChild(dec);
//4、创建根节点
XmlElement books=doc.CreateElement("Books");
//将根节点添加到文档中
doc.AppendChile(books);
//5、给根节点Books创建子节点
XmlElement book1=doc.CreateElement("Book");
//将book1添加到根节点
books.AppendChild(book1);

//6、给book1添加子节点
XmlElement name1=doc.CreateElement("Name");
name1.InnerText="金**";
book1.AppendChild(name1);

XmlElement price1=doc.CreateElement("Price");
price1.InnerText="10";
book1.AppendChild(price1);

XmlElement des1=doc.CreateElement("Des");
des1.InnerText="好看";
book1.AppendChild(des1);


doc.Save("Books.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Order>
<CustomeName>刘洋</CustomeName>
<CustomerNumber>刘洋</CustomerNumber>
<Items>
<OrderItem Name="码表" Count="2" />
<OrderItem Name="雨衣" Count="40" />
<OrderItem Name="手套" Count="1" />
</Items>
</Order>

//创建带属性的xml文档
XmlDocument doc=new XmlDocument()
XmlDeclaration dec=doc.CreateXmlDeclaration("1.0","utf-8",null);
doc.AppendChild(dec);

XmlElement order=doc.CreateElement("Order");
doc.AppendChild(order);

XmlElement customerName=doc.CreateElement("CustomerName");
customerName.InnerText="颜世伟";
order.AppendChild(customerName);

XmlElement customerNumber=doc.CreateElement("CustomerNumber");
customerNumber.InnerText="100001";
order.AppendChild(customerNumber);

XmlElement items=doc.CreateElement("Items");
order.AppendChild(items);

XmlElement orderItem1=doc.CreateElement("OrderItem");
//给节点添加属性
orderItem1.SetAttribute("Name","充气娃娃");
orderItem1.SetAttribute("Count","10");
items.AppendChild(orderItem1);

XmlElement orderItem2=doc.CreateElement("OrderItem");
//给节点添加属性
orderItem2.SetAttribute("Name","充气娃娃");
orderItem2.SetAttribute("Count","10");
items.AppendChild(orderItem2);

XmlElement orderItem3=doc.CreateElement("OrderItem");
//给节点添加属性
orderItem3.SetAttribute("Name","充气娃娃");
orderItem3.SetAttribute("Count","10");
items.AppendChild(orderItem3);

doc.Save("Order.xml");


-----------------------------追加xml文档------------------------------------------------------
//追加xml文档
XmlDocument doc=new XmlDocument();
XmlElement books;
if(File.Exists("Books.xml")){
//如果文件存在 加左xml
doc.Load("Books.xml");
//获得了文件的根节点
books=doc.DocumentElement;
}else{
//如果文件不存在
//创建第一行
XmlDeclaration dec=doc.ReateXmlDeclaration("1.0","utf-8",null);
doc.AppendChild(dec);
//创建根节点
books=doc.CreateElement("Books");
doc.AppendChild(books);
}
XmlElement book1=doc.CreateElement("Book");
//将book1添加到根节点
books.AppendChild(book1);

XmlElement name1=doc.CreateElement("Name");
name1.InnerText="C#开发大全";
book1.AppendChild(name1);

XmlElement price1=doc.CreateElement("Price");
price1.InnerText="101";
book1.AppendChild(price1);

XmlElement des1=doc.CreateElement("Des");
des1.InnerText="看不懂";
book1.AppendChild(des1);

doc.Save("Books.xml");


------------------------4、读取XML文档--------------------------------------
XmlDocument doc=new XmlDocument();
//加载要读取的xml
doc.Load("Books.xml");
//获取根节点
XmlElement books=doc.DocumentElement;
//获取子节点,返回节点的集合
XmlNodeList xnl=books.ChildNodes;
foreach(XmlNode item in xnl)
{
Console.WriteLine(item.InnerText);
}
Console.ReadKey();


----------------------5、读取带属性的XML文档-----------------------------------
XmlDocument doc =new XmlDocument();
doc.Load("Order.xml");
XmlNodeList xnl=doc.SelectNodes("/Order/Items/OrderItem");
foreach(XmlNode node in xnl)
{
Console.WriteLine(node.Attributes["Name"].Value);
Console.WriteLine(node.Attributes["Count"].Value);
}


---------------------6、删除节点---------------------------------------------

XmlDocument doc =new XmlDocument();
doc.Load("Order.xml");
XmlNode xn=doc.SelectSingleNode("/Order/Items");
xn.RemoveAll();
doc.Save("Order.xml");

Console.WriteLine("删除成功");

原文地址:https://www.cnblogs.com/iceberg2008/p/4244575.html