C# 对XML基本操作代码总结
程序员文章站
2023-11-30 08:23:52
具体如下: xml文件:文件在mydocument文件夹下 复制代码 代码如下: ...
具体如下:
xml文件:文件在mydocument文件夹下
<?xml version="1.0" encoding="utf-8"?>
<personf xmlns="" name="(test)work hard work smart!">
<person name="person1">
<id>1</id>
<name>xiaoa</name>
<age>59</age>
</person>
<person name="person2">
<id>2</id>
<name>xiaob</name>
<age>29</age>
</person>
<person name="person3">
<id>3</id>
<name>xiaoc</name>
<age>103</age>
</person>
<person name="person4">
<id>4</id>
<name>xiaod</name>
<age>59</age>
</person>
</personf>
code:说明都在注释里。
void testxml()
{
xmldocument doc = new xmldocument();
string path = "//www.jb51.net/mydocument/person.xml";
try
{
doc.load(path);
//1、读取单个节点的数据
xmlnode node = doc.selectsinglenode("personf");
//2、读取多个节点的数据
xmlnodelist nodelist1 = doc.selectnodes("personf/person");
//3.1 读取具体节点的具体值 如:属性为person2的第二个节点name的innertext
xmlnodelist nodelist = doc.documentelement.getelementsbytagname("person");
foreach (xmlnode node2 in nodelist1) //当然也能用nodelist的值
{
if (node2.attributes["name"].innertext == "person2")
{
console.writeline(node2.childnodes[1].innertext);
}
}
//3.2 读取id为2所在的节点第二个子节点name的innertext
xmlnode node3 = doc.selectsinglenode("personf/person[id=2]");
string strnode3 = node3.childnodes[1].innertext;
//3.3利用下面的方法可以找到id为2的节点
xmlnodelist nodelist2 = doc.selectnodes("//person//id");
xmlnode node4 = null;
foreach (xmlnode node5 in nodelist2)
{
if (node5.innertext == "2")
{
node4 = node5;
break;
}
}
console.writeline(node4.innertext);
//4、读取节点的属性
string name = node.attributes["name"].innertext;
//5 修改节点的属性
node.attributes["name"].innertext = "work hard work smart!";
doc.save(path);
//6 添加自定义的节点
xmltextreader reader = new xmltextreader(path);
xmlelement root = doc.documentelement;//获取根节点
xmlelement tagouter = doc.createelement("person");
xmlelement tagin = doc.createelement("name");
tagin.innertext = "work hard work smart!";
tagouter.appendchild(tagin);
root.appendchild(tagouter);//添加tagouter到xml文件的最后
reader.close();
doc.save(path);
}
catch (system.exception e)
{
throw new exception(e.message);
}
}
xml文件:文件在mydocument文件夹下
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<personf xmlns="" name="(test)work hard work smart!">
<person name="person1">
<id>1</id>
<name>xiaoa</name>
<age>59</age>
</person>
<person name="person2">
<id>2</id>
<name>xiaob</name>
<age>29</age>
</person>
<person name="person3">
<id>3</id>
<name>xiaoc</name>
<age>103</age>
</person>
<person name="person4">
<id>4</id>
<name>xiaod</name>
<age>59</age>
</person>
</personf>
code:说明都在注释里。
复制代码 代码如下:
void testxml()
{
xmldocument doc = new xmldocument();
string path = "//www.jb51.net/mydocument/person.xml";
try
{
doc.load(path);
//1、读取单个节点的数据
xmlnode node = doc.selectsinglenode("personf");
//2、读取多个节点的数据
xmlnodelist nodelist1 = doc.selectnodes("personf/person");
//3.1 读取具体节点的具体值 如:属性为person2的第二个节点name的innertext
xmlnodelist nodelist = doc.documentelement.getelementsbytagname("person");
foreach (xmlnode node2 in nodelist1) //当然也能用nodelist的值
{
if (node2.attributes["name"].innertext == "person2")
{
console.writeline(node2.childnodes[1].innertext);
}
}
//3.2 读取id为2所在的节点第二个子节点name的innertext
xmlnode node3 = doc.selectsinglenode("personf/person[id=2]");
string strnode3 = node3.childnodes[1].innertext;
//3.3利用下面的方法可以找到id为2的节点
xmlnodelist nodelist2 = doc.selectnodes("//person//id");
xmlnode node4 = null;
foreach (xmlnode node5 in nodelist2)
{
if (node5.innertext == "2")
{
node4 = node5;
break;
}
}
console.writeline(node4.innertext);
//4、读取节点的属性
string name = node.attributes["name"].innertext;
//5 修改节点的属性
node.attributes["name"].innertext = "work hard work smart!";
doc.save(path);
//6 添加自定义的节点
xmltextreader reader = new xmltextreader(path);
xmlelement root = doc.documentelement;//获取根节点
xmlelement tagouter = doc.createelement("person");
xmlelement tagin = doc.createelement("name");
tagin.innertext = "work hard work smart!";
tagouter.appendchild(tagin);
root.appendchild(tagouter);//添加tagouter到xml文件的最后
reader.close();
doc.save(path);
}
catch (system.exception e)
{
throw new exception(e.message);
}
}