xml文件格式如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
userdata
createuser
="false"
>
<
dataconnection
>
<
server
>
localhost
</
server
>
<
uid
>
sa
</
uid
>
<
pwd
></
pwd
>
</
dataconnection
>
<
net
>
<
name
>
jiayuan
</
name
>
</
net
>
</
userdata
>
读取节点中的一个属性
XmlDocument doc
=
new
XmlDocument();
doc.Load(
"
config.xml
"
);
//
可以再加入路径:如D:config.xml
XmlNode xnuser
=
doc.SelectSingleNode(
"
userdata
"
);
string
flag
=
xnuser.Attributes[
"
createuser
"
].InnerText;
读取节点中的值
XmlDocument doc
=
new
XmlDocument();
doc.Load(
"
config.xml
"
);
XmlNode xnserver
=
doc.SelectSingleNode(
"
userdata/dataconnection/server
"
);
修改节点的属性
XmlDocument doc
=
new
XmlDocument();
doc.Load(
"
config.xml
"
);
XmlNode xnuser
=
doc.SelectSingleNode(
"
userdata
"
);
xnuser.Attributes[
"
createuser
"
].InnerText
=
"
false
"
;
doc.Save(
"
config.xml
"
);
追加节点
XmlDocument doc
=
new
XmlDocument();
XmlTextReader reader
=
new
XmlTextReader(
"
config.xml
"
);
doc.Load(
"
config.xml
"
);
XmlElement root
=
doc.DocumentElement;
//
获取根节点
XmlElement tagMessage
=
doc.CreateElement(
"
net
"
);
XmlElement tagText
=
doc.CreateElement(
"
name
"
);
tagText.InnerText
=
netname;
tagMessage.AppendChild(tagText);
//
追加到 xml 文本的最后面
root.AppendChild(tagMessage);
reader.Close();
//
关闭 XmlTextReader
doc.Save(
"
config.xml
"
);
//
保存 xml 文件
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
1. XML文档操作
1.1 加载一个XML文档
Dim objDoc As New XmlDocument()
objDoc.Load("Sample.xml")
1.2 加载一个XML数据
Dim objDoc As New XmlDocument()
Dim strXML As String
strXML = "<Employees><Employee id=”12345”><Employee_ID>12345</Employee_ID><Name>Zhang Bin</Name></Employee></Employees>"
objDoc.Load(strXML)
1.3 保存文档
objDoc.Save("Sample.xml")
2 XML 数据的查询
最常见的XML数据类型有:Element, Attribute,Comment, Text.
Element, 指形如<Name>Tom<Name>的节点。它可以包括:Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference.
Attribute, 指在<Employee id=”12345”>中的粗体部分。
Comment,指形如:<!-- my comment --> 的节点。
Text,指在<Name> Tom<Name>的粗体部分。
在XML中,可以用XmlNode对象来参照各种XML数据类型。
2.1 查询已知绝对路径的节点(集)
objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee”)
或者
objNodeList = objNode.SelectNodes(“/Company/Department/Employees/Employee”)
以上两种方法可返回一个NodeList对象,如果要返回单个节点可使用SelectSingleNode方法,该方法如果查询到一个或多个节点,返回第一个节点;如果没有查询的任何节点返回 Nothing。例如:
objNode = objNode.SelectSingleNode(“/Company/Department/Employees/Employee”)
If Not (objNode is Nothing) then
‘- Do process
End If
2.2 查询已知相对路径的节点(集)
可使用类似于文件路径的相对路径的方式来查询XML的数据
objNode = objDoc.SelectSingleNode(“Company/Department”)
objNodeList = objNode.SelectNodes(“../Department)
objNode = objNode.SelectNode(“Employees/Employee”)
2.3 查询已知元素名的节点(集)
在使用不规则的层次文档时,由于不知道中间层次的元素名,可使用//符号来越过中间的节点,查询其子,孙或多层次下的其他所有元素。例如:
objNodeList = objDoc.SelectNodes(“Company//Employee”)
2.4 查询属性(attribute)节点
以上的各种方法都返回元素(element)节点(集),返回属性(attribute),只需要采用相应的方法,在属性名前加一个@符号即可,例如:
objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee/@id”)
objNodeList = objDoc.SelectNodes(“Company//@id”)
2.5 查询Text节点
使用text()来获取Text节点。
objNode = objDoc.SelectSingleNode(“Company/Department/Deparmt_Name/text()”)
2.6 查询特定条件的节点
使用[]符号来查询特定条件的节点。例如:
a. 返回id号为10102的Employee节点
objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee[@id=’10102’]”)
b. 返回Name为Zhang Qi的Name 节点
objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee/Name[text()=’Zhang Qi’]”)
c. 返回部门含有职员22345的部门名称节点
objNode = objDoc.SelectSingleNode("Company/Department[Employees/Employee/@id='22345']/Department_Name")
2.7 查询多重模式的节点
使用 | 符号可以获得多重模式的节点。例如:
objNodeList = objDoc.SelectNodes(“Company/Department/Department_Name | Company/Department/Manager”)
2.8 查询任意子节点
使用*符号可以返回当前节点的所有子节点。
objNodeList = objDoc.SelectNodes(“Company/*/Manager)
或者
objNodeList = objNode.ChildNodes
3 XML数据的编辑
3.1 增加一个元素的属性(attribute)节点
Dim objNodeAttr As XmlNode
objNodeAttr = objDoc.CreateAttribute("id", Nothing)
objNodeAttr.InnerXml = "101"
objNode.Attributes.Append(objNodeAttr)
3.2 删除一个元素的属性
objNode.Attributes.Remove(objNodeAttr)
3.3 增加一个子元素(Element)
Dim objNodeChild As XmlNode
objNodeChild = objDoc.CreateElement(Nothing, "ID", Nothing)
objNodeChild.InnerXml = "101"
objNode.AppendChild(objNodeChild)
3.4 删除一个子元素
objNode.RemoveChild(objNodeChild)
3.5 替换一个子元素
objNOde.ReplaceChild(newChild,oldChild)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
1. XML文档操作
1.1 加载一个XML文档
Dim objDoc As New XmlDocument()
objDoc.Load("Sample.xml")
1.2 加载一个XML数据
Dim objDoc As New XmlDocument()
Dim strXML As String
strXML = "<Employees><Employee id=”12345”><Employee_ID>12345</Employee_ID><Name>Zhang Bin</Name></Employee></Employees>"
objDoc.Load(strXML)
1.3 保存文档
objDoc.Save("Sample.xml")
2 XML 数据的查询
最常见的XML数据类型有:Element, Attribute,Comment, Text.
Element, 指形如<Name>Tom<Name>的节点。它可以包括:Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference.
Attribute, 指在<Employee id=”12345”>中的粗体部分。
Comment,指形如:<!-- my comment --> 的节点。
Text,指在<Name> Tom<Name>的粗体部分。
在XML中,可以用XmlNode对象来参照各种XML数据类型。
2.1 查询已知绝对路径的节点(集)
objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee”)
或者
objNodeList = objNode.SelectNodes(“/Company/Department/Employees/Employee”)
以上两种方法可返回一个NodeList对象,如果要返回单个节点可使用SelectSingleNode方法,该方法如果查询到一个或多个节点,返回第一个节点;如果没有查询的任何节点返回 Nothing。例如:
objNode = objNode.SelectSingleNode(“/Company/Department/Employees/Employee”)
If Not (objNode is Nothing) then
‘- Do process
End If
2.2 查询已知相对路径的节点(集)
可使用类似于文件路径的相对路径的方式来查询XML的数据
objNode = objDoc.SelectSingleNode(“Company/Department”)
objNodeList = objNode.SelectNodes(“../Department)
objNode = objNode.SelectNode(“Employees/Employee”)
2.3 查询已知元素名的节点(集)
在使用不规则的层次文档时,由于不知道中间层次的元素名,可使用//符号来越过中间的节点,查询其子,孙或多层次下的其他所有元素。例如:
objNodeList = objDoc.SelectNodes(“Company//Employee”)
2.4 查询属性(attribute)节点
以上的各种方法都返回元素(element)节点(集),返回属性(attribute),只需要采用相应的方法,在属性名前加一个@符号即可,例如:
objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee/@id”)
objNodeList = objDoc.SelectNodes(“Company//@id”)
2.5 查询Text节点
使用text()来获取Text节点。
objNode = objDoc.SelectSingleNode(“Company/Department/Deparmt_Name/text()”)
2.6 查询特定条件的节点
使用[]符号来查询特定条件的节点。例如:
a. 返回id号为10102的Employee节点
objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee[@id=’10102’]”)
b. 返回Name为Zhang Qi的Name 节点
objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee/Name[text()=’Zhang Qi’]”)
c. 返回部门含有职员22345的部门名称节点
objNode = objDoc.SelectSingleNode("Company/Department[Employees/Employee/@id='22345']/Department_Name")
2.7 查询多重模式的节点
使用 | 符号可以获得多重模式的节点。例如:
objNodeList = objDoc.SelectNodes(“Company/Department/Department_Name | Company/Department/Manager”)
2.8 查询任意子节点
使用*符号可以返回当前节点的所有子节点。
objNodeList = objDoc.SelectNodes(“Company/*/Manager)
或者
objNodeList = objNode.ChildNodes
3 XML数据的编辑
3.1 增加一个元素的属性(attribute)节点
Dim objNodeAttr As XmlNode
objNodeAttr = objDoc.CreateAttribute("id", Nothing)
objNodeAttr.InnerXml = "101"
objNode.Attributes.Append(objNodeAttr)
3.2 删除一个元素的属性
objNode.Attributes.Remove(objNodeAttr)
3.3 增加一个子元素(Element)
Dim objNodeChild As XmlNode
objNodeChild = objDoc.CreateElement(Nothing, "ID", Nothing)
objNodeChild.InnerXml = "101"
objNode.AppendChild(objNodeChild)
3.4 删除一个子元素
objNode.RemoveChild(objNodeChild)
3.5 替换一个子元素
objNOde.ReplaceChild(newChild,oldChild)