当前位置: 移动技术网 > IT编程>开发语言>c# > C# 操作XML文档 使用XmlDocument类方法

C# 操作XML文档 使用XmlDocument类方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
w3c制定了xml dom标准。很多编程语言中多提供了支持w3c xml dom标准的api。我在之前的文章中介绍过如何使用javascript对xml文档进行加载与查询。
w3c制定了xml dom标准。很多编程语言中多提供了支持w3c xml dom标准的api。我在之前的文章中介绍过如何使用javascript对xml文档进行加载与查询。在本文中,我来介绍一下.net中的xmldocument类。它支持并扩展了w3c xml dom标准。它将整个xml文档都先装载进内存中,然后再对xml文档进行操作,所以如果xml文档内容过大,不建议使用xmldocument类,因为会消耗过多内存。对于很大的xml文档,可以使用xmlreader类来读取。因为xmlreader使用steam(流)来读取文件,所以不会对内存造成太大的消耗。下面就来看一下如何使用xmldocument类。
(一) 加载
加载xml比较常用的有三种方法:
public virtual void load(string filename);
public virtual void load(stream instream);
public virtual void loadxml(string xml);
下面代码演示如何使用它们:
复制代码 代码如下:

xmldocument xmldoc = new xmldocument();
xmldoc.load("xmlfile1.xml");
entity retrievedannotation = _orgservice.retrieve("annotation"
, new guid("c1b13c7f-f430-e211-8fa1-984be1731399"), new columnset(true));
byte[] filecontent = convert.frombase64string(retrievedannotation["documentbody"].tostring());
memorystream ms = new memorystream(filecontent);
xmldocument xmldoc2 = new xmldocument();
xmldoc2.load(ms);
string str = @"<customers><customer id='01' city='beijing' country='china' name='lenovo'/></customers>";
xmldocument xmldoc3 = new xmldocument();
xmldoc3.loadxml(str);

(二) 查询
对xml的元素、属性、文本的查询可以使用xpath。具体的定义可以参看w3school。
首先应该了解一下xpath表达式:
表达式 描述
nodename 选取此节点的所有子节点。
/ 从根节点选取。
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点。
.. 选取当前节点的父节点。
@ 选取属性。

我们主要使用两个方法来查询xml文档,selectnodes(xpath expression)和selectsinglenode(xpath expression)。
selectnodes返回一个xmlnodelist对象,也就是所有符合xpath表达式的xml节点都将会被返回,你需要对返回的结果进行遍历。
selectsinglenode只返回第一个符合xpath表达式的节点,或者返回null。
以下面的xml文件为例,我们进行一些演示:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<customers>
<customer id="01" city="beijing" country="china" name="lenovo">
<contact gender="female" title="support">li li</contact>
</customer>
<customer id="02" city="amsterdam" country="the netherlands" name="shell">
<contact gender="male" title="sales person">aaron babbitt</contact>
<contact gender="female" title="sales manager">daisy cabell</contact>
<contact gender="male" title="sales person">gabriel eads</contact>
</customer>
</customers>

1. 返回所有contact节点:
xmlnodelist nodelist = xmldoc.selectnodes("/customers/customer/contact");
foreach (xmlnode node in nodelist)
{
console.writeline(node.outerxml);
}
输出结果为:
<contact gender="female" title="support">li li</contact>
<contact gender="male" title="sales person">aaron babbitt</contact>
<contact gender="female" title="sales manager">daisy cabell</contact>
<contact gender="male" title="sales person">gabriel eads</contact>
2. 返回id为02的customer:
xmlnode node = xmldoc.selectsinglenode("/customers/customer[@id='02']");
console.writeline(node.outerxml);
输出结果为:
<customer id="02" city="amsterdam" country="the netherlands" name="shell">
<contact gender="male" title="sales person">aaron babbitt</contact>
<contact gender="female" title="sales manager">daisy cabell</contact>
<contact gender="male" title="sales person">gabriel eads</contact>
</customer>
3. 返回含有contact名为li li的contact:
xmlnode node = xmldoc.selectsinglenode("/customers/customer/contact[text()='li li']");
console.writeline(node.outerxml);
输出结果:
<contact gender="female" title="support">li li</contact>
4. 返回含有contact名为 li li 的customer。注意和3的区别:
xmlnode node = xmldoc.selectsinglenode("/customers/customer[contact/text()='li li']");
console.writeline(node.outerxml);
输出结果:
<customer id="01" city="beijing" country="china" name="lenovo">
<contact gender="female" title="support">li li</contact>
</customer>
5. (1) 获取outer xml:
xmlnode node = xmldoc.selectsinglenode("/customers/customer[@id='02']");
console.writeline(node.outerxml);
(2) 获取 inner xml:
xmlnode node = xmldoc.selectsinglenode("/customers/customer[@id='02']");
console.writeline(node.innerxml);
(3) 获取 text
xmlnode node = xmldoc.selectsinglenode("/customers/customer/contact[text()='li li']");
console.writeline(node.innertext);
(4) 获取属性
xmlnode node = xmldoc.selectsinglenode("/customers/customer/contact[text()='li li']");
console.writeline(node.attributes["gender"].value);
(三) 创建
以创建以下xml文档为例:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<customers>
<customer id="01" name="lenovo" country="china" city="beijing">
<contact title="support" gender="female">li li</contact>
</customer>
</customers>

复制代码 代码如下:

var xmldoc = new xmldocument();
//create the xml declaration first
xmldoc.appendchild(xmldoc.createxmldeclaration("1.0", "utf-8", null));
//create the root node and append into doc
var el = xmldoc.createelement("customers");
xmldoc.appendchild(el);
// customer lenovo
xmlelement elementcustomer = xmldoc.createelement("customer");
xmlattribute attrid = xmldoc.createattribute("id");
attrid.value = "01";
elementcustomer.attributes.append(attrid);
xmlattribute cityid = xmldoc.createattribute("city");
cityid.value = "beijing";
elementcustomer.attributes.append(cityid);
xmlattribute attrcountry = xmldoc.createattribute("country");
attrcountry.value = "china";
elementcustomer.attributes.append(attrcountry);
xmlattribute namecountry = xmldoc.createattribute("name");
namecountry.value = "lenovo";
elementcustomer.attributes.append(namecountry);
el.appendchild(elementcustomer);
// contact li li
xmlelement elementcontact = xmldoc.createelement("contact");
elementcontact.innertext = "li li";
xmlattribute attrgender = xmldoc.createattribute("gender");
attrgender.value = "female";
elementcontact.attributes.append(attrgender);
xmlattribute titlegender = xmldoc.createattribute("title");
titlegender.value = "support";
elementcontact.attributes.append(titlegender);
elementcustomer.appendchild(elementcontact);
xmldoc.save("test.xml");

总结: xmldocument类是.net api中提供的支持w3c xml dom标准的类。可以用它来创建和查询xml文档。由于xmldocument要将xml文档的内容全部装载进内存中,所以对于读取内容过大的xml文档,不适合使用xmldocument类,而可以使用xmlreader来完成读取。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网