C#探秘系列(七):XML文件操作(一)
XML 是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分。你可以创建内容,然后使用限定标记标记它,从而使每个单词、短语或块成为可识别、可分类的信息。因而在C#中,对xml文件的操作尤为重要。本博客着力于在C#中如何对xml文件实施诸如创建、搜索、序列化和反序列化等操作,默认你已有一定的xml基础。
xml的基础知识可参照下面博客:
http://www.ibm.com/developerworks/cn/xml/x-newxml/
下面介绍的操作均基于Customer对象:
<Customer>
<FirstName>Orlando</FirstName>
<LastName>Gee</LastName>
<EmailAddress>orlando0@hotmail.com</EmailAddress>
</Customer>
一、利用C#创建xml文档
创建xml文件需要四步:
(一)新建xml文档: XmlDocument();
(二)创建根结点: CreateElement();
(三)添加子元素: AppendChild();
(四)添加元素属性: CreateElement(), AppendChild()。
首先,要使用C#中定义的操作方法,必须引入命名空间,即:
using System.Xml
1、新建xml文档
XmlDocument customerXml = new XmlDocument();
2、创建根结点并将其添加
XmlElement rootElem = customerXml.CreateElement("Customers");
customerXml.AppendChild(rootElem);
3、添加子元素
XmlElement customerElem = customerXml.CreateElement("Customer");
4、添加元素属性
XmlElement firstNameElem = customerXml.CreateElement("FirstName");
firstNameElem.InnerText = customer.FirstName;
customerElem.AppendChild(firstNameElem);
5、实例代码及输出
//Create a list of customers
List<Customer> customers = CreateCustomerList();
//Create a XmlDocument
XmlDocument customerXml = new XmlDocument();
//Create a root element
XmlElement rootElem = customerXml.CreateElement("Customers");
customerXml.AppendChild(rootElem);
foreach (Customer customer in customers)
{
//Create new element representing the customer object.
XmlElement customerElem = customerXml.CreateElement("Customer");
//Add element “FirstName” property to the customer element.
XmlElement firstNameElem = customerXml.CreateElement("FirstName");
firstNameElem.InnerText = customer.FirstName;
customerElem.AppendChild(firstNameElem);
//Add element: LastName property to the customer element.
XmlElement lastNameElem = customerXml.CreateElement("LastName");
lastNameElem.InnerText = customer.LastName;
customerElem.AppendChild(lastNameElem);
//Add element: EmailAddress property to the customer element.
XmlElement emailAddress = customerXml.CreateElement("EmailAddress");
emailAddress.InnerText = customer.EmailAddress;
customerElem.AppendChild(emailAddress);
//Finally add the customer element to the XML document
rootElem.AppendChild(customerElem);
}
Console.WriteLine(customerXml.OuterXml);
Console.Read();
输出结果:
<Customers>
<Customer>
<FirstName>Orlando</FirstName>
<LastName>Gee</LastName>
<EmailAddress>orlando0@hotmail.com</EmailAddress>
</Customer>
<Customer>
<FirstName>Keith</FirstName>
<LastName>Harris</LastName>
<EmailAddress>keith0@hotmail.com</EmailAddress>
</Customer>
<> …</>
</Customers>
如果想将customer的属性添加至元素表头,如:
<Customer FirstName="Orlando" LastName="Gee">
<EmailAddress>…</EmailAddress>
</Customer>
可以使用CreateAttribute()方法:
// Add an attribute :FirstName property to the customer element.
XmlAttribute firstNameAttr = customerXml.CreateAttribute("FirstName");
firstNameAttr.Value = customer.FirstName;
customerElem.Attributes.Append(firstNameAttr);
二、利用XPath在xml文件中搜索节点或属性
1.使用绝对路径搜索
XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,有关其详细语法参照:
http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html
搜索的结果包括两种:XmlNode与XmlEelment,其继承关系如下:
System.Object
System.Xml.XmlNode
System.Xml.XmlLinkedNode
System.Xml.XmlElement
在XmlNode类中主要用到了两个函数用来实现搜索:
SelectSingleNode(String xpath)与SelecNodes(String xpath),差别无非是单复数的问题。
先来一段代码:
string xPath = "/Customers/Customer[@FirstName='Donna']";
XmlNode oneCustomer = customerXml.SelectSingleNode(xPath);
其中第一个/表示从根节点开始搜索,第二个/就表示根节点之后的子节点了。[ ]表示搜索条件,@表示对应的属性。这样就从xml文件中搜索到了一个满足条件的xml节点。
代码示例:
string xPath = "/Customers/Customer[@FirstName='Donna']";
XmlNode oneCustomer = customerXml.SelectSingleNode(xPath);
Console.WriteLine("\nSelectSingleNode(\"{0}\")...", xPath);
if (oneCustomer != null)
Console.WriteLine(oneCustomer.OuterXml);
else
Console.WriteLine("Not found");
xPath = "/Customers/Customer[@FirstName='Donna']";
XmlElement customerElem = customerXml.SelectSingleNode(xPath) as XmlElement;
Console.WriteLine("\nSelectSingleNode(\"{0}\")...", xPath);
if (customerElem != null)
{
Console.WriteLine(customerElem.OuterXml);
Console.WriteLine("customerElem.HasAttributes = {0}", customerElem.HasAttributes);
}
else
Console.WriteLine("Not found");
输出结果:
SelectSingleNode("/Customers/Customer[@FirstName='Donna']")...
<Customer FirstName="Donna" LastName="Carreras">
<EmailAddress>donna0@adventure-works.com</EmailAddress>
</Customer>
SelectSingleNode("/Customers/Customer[@FirstName='Donna']")...
<Customer FirstName="Donna" LastName="Carreras">
<EmailAddress>donna0@adventure-works.com</EmailAddress>
</Customer>
customerElem.HasAttributes = True
2、使用相对路径:Axes坐标轴
如果我并不了解xml文件的构造方式,那就无法用绝对路径来搜索了,相对路径就派上用场了:
xPath = "descendant::Customer[@FirstName='Donna']";
oneCustomer = customerXml.SelectSingleNode(xPath);
其中descendant表示搜索所在位置后的所有符合条件的后代,即子结点。
示例代码:
xPath = "descendant::Customer[@FirstName='Donna']";
oneCustomer = customerXml.SelectSingleNode(xPath);
Console.WriteLine("\nSelectSingleNode(\"{0}\")...", xPath);
if (oneCustomer != null)
Console.WriteLine(oneCustomer.OuterXml);
else
Console.WriteLine("Not found");
输出结果:
SelectSingleNode("descendant::Customer[@FirstName='Donna']")...
<Customer FirstName="Donna" LastName="Carreras">
<EmailAddress>donna0@adventure-works.com</EmailAddress>
</Customer>
3、使用XPath函数确定条件
xPath = "descendant::Customer[starts-with(@LastName, 'G') " +
"and contains(EmailAddress, 'adventure-works.com')]";
customers = customerXml.SelectNodes(xPath);
Console.WriteLine("\nSelectNodes(\"{0}\")...", xPath);
if (customers != null)
{
foreach (XmlNode customer in customers)
Console.WriteLine(customer.OuterXml);
}
else
Console.WriteLine("Not found");
常用的XPath包括:
start-with()表示传入的属性以某个字段开头//Customer[starts_with(@LastName, ‘G’)]表示取lasName以’G’打头的customer节点;
contains()表示包含第二个参数描述的属性的节点//Customer[contains(EmailAddress, ‘adventure-works.com’)]表示取EmailAddress为adventure-works.com的customer
position() 表示节点的序号例如 //cat[position() = 2] 表示取序号为2的dog节点;
last() 表示取最后一个节点 //cat[last()] ;
name() 表示当前节点名字 /pets/*[name() != ‘pig’] 表示/pets下名字不是pig的子节点。
故输出结果为:
SelectNodes("descendant::Customer[starts-with(@LastName, 'G') and contains(EmailAddress, 'adventure-works.com')]")...
<Customer FirstName="Orlando" LastName="Gee">
<EmailAddress>orlando0@adventure-works.com</EmailAddress>
</Customer>
<Customer FirstName="Janet" LastName="Gates">
<EmailAddress>janet1@adventure-works.com</EmailAddress>
</Customer>
暂驻笔于此,洒家且歇歇~