C#探秘系列(七):XML文件操作(一)

摘要:
因而在C#中,对xml文件的操作尤为重要。本博客着力于在C#中如何对xml文件实施诸如创建、搜索、序列化和反序列化等操作,默认你已有一定的xml基础。xml的基础知识可参照下面博客:http://www.ibm.com/developerworks/cn/xml/x-newxml/下面介绍的操作均基于Customer对象:OrlandoGeeorlando0@hotmail.com一、利用C#创建xml文档创建xml文件需要四步:(一)新建xml文档:XmlDocument();(二)创建根结点:CreateElement();(三)添加子元素:AppendChild();(四)添加元素属性:CreateElement(),AppendChild()。

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("
SelectSingleNode("{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("
SelectSingleNode("{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("
SelectSingleNode("{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("
SelectNodes("{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>

暂驻笔于此,洒家且歇歇~

免责声明:文章转载自《C#探秘系列(七):XML文件操作(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇openpyxl/csv--python处理excel表格模块LeetCode 1366. 通过投票对团队排名 哈希排序下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

在.xml文件中模糊查询的常用的3种方法

我经常使用的模糊查询的方法是: <!-- ******************** 模糊查询的常用的3种方式:********************* --> <select parameterType="User" resultType="User"> select <include refid...

nuxt服务端渲染怎么引入element ui

第一步,安装element ui npm install --save element-ui 第二步骤 在plugins文件底下新建一个js---element.js 代码 import Vue from 'vue' import Element from 'element-ui' import 'element-ui/lib/theme-chalk/i...

用友GRP-u8 注入-RCE漏洞复现

​ ​用友GRP-u8 注入-RCE漏洞复现 (HW第一时间复现了,当时觉得不合适,现在才发出来) 一、漏洞简介 用友GRP-u8存在XXE漏洞,该漏洞源于应用程序解析XML输入时没有进制外部实体的加载,导致可加载恶意外部文件。 二、漏洞复现 SQL注入POC POST /Proxy HTTP/1.1Host:localhost:8080Upgrade-...

selenium 通过xpath进行元素定位--By.xpath()

xpath是XML路径语言,它可以用来确定xml文档中的元素位置,通过元素的路径来完成对元素的查找。HTML就是XML的一种实现方式,所以xpath是一种非常强大的定位方式。xpath也分几种不同类型的定位方法。 一种是绝对路径定位。这种定位方式是利用html标签名的层级关系来定位元素的绝对路径,一般从<html>标签开始依次往下进行查找。如百...

R实战之热点图(HeatMap)

快速实现是搜索帮助文档的首要目的,所以此处涉及实战的文章一概略去传统帮助文档的理论部分,直接上代码加注释! 本文将介绍R语言下利用ggplot2包制作heatmap的代码 -------------------------我是分割线------------------------------- 测试数据: 数据中的空白行、列用以分割heatmap,区别不...

主题:android之XmlResourceParser类使用实例 转

http://www.iteye.com/topic/1122056 android开发过程会经常使用XML文件,下面提供小实例,看看XmlResourceParser类是怎么样解析XML文件的。 XML文件名称为test.xml。内容如下: Xml代码 <?xmlversion="1.0"encoding="utf-8"?> <...