XML相关操作
相关XML
1<?xml version="1.0" encoding="gb2312"?>
2 <userList>
3 <userInfo>
4 <user_id>001</user_id>
5 <name>孙吉</name>
6 <q_id>200602070001</q_id>
7 <q_name>我们是一家人</q_name>
8 <check_time>2006-2-10</check_time>
9 </userInfo>
10 <userInfo>
11 <user_id>002</user_id>
12 <name>张三</name>
13 <q_id>200602070004</q_id>
14 <q_name>天天快乐</q_name>
15 <check_time>2006-2-16</check_time>
16 </userInfo>
17 </userList>
18
2 <userList>
3 <userInfo>
4 <user_id>001</user_id>
5 <name>孙吉</name>
6 <q_id>200602070001</q_id>
7 <q_name>我们是一家人</q_name>
8 <check_time>2006-2-10</check_time>
9 </userInfo>
10 <userInfo>
11 <user_id>002</user_id>
12 <name>张三</name>
13 <q_id>200602070004</q_id>
14 <q_name>天天快乐</q_name>
15 <check_time>2006-2-16</check_time>
16 </userInfo>
17 </userList>
18
如:
(1)、插入的一条记录值依次为:003、李四、200602070005、永远幸福、2006-2-17
(2)、删除的user_id为“002”的那条记录。
(3)、更新user_id为“001”记录的“q_name”为“一家人”。
XmlDocument _doc = new XmlDocument();
_doc.Load(@"test.xml");
//新增
XmlElement _eleParent = _doc.CreateElement("userInfo");
XmlElement _eleId = _doc.CreateElement("user_id");
_eleId.InnerText = "003";
XmlElement _eleName = _doc.CreateElement("Name");
_eleName.InnerText = "李四";
XmlElement _eleQid = _doc.CreateElement("q_id");
_eleQid.InnerText = "0200602070005";
XmlElement _eleQName = _doc.CreateElement("q_Name");
_eleQName.InnerText = "永远幸福";
XmlElement _eleTime = _doc.CreateElement("check_time");
_eleTime.InnerText = "2006-2-17";
_eleParent.AppendChild(_eleId);
_eleParent.AppendChild(_eleName);
_eleParent.AppendChild(_eleQid);
_eleParent.AppendChild(_eleQName);
_eleParent.AppendChild(_eleTime);
_doc.DocumentElement.AppendChild(_eleParent);
_doc.Save(@"new.xml");
_doc.Load(@"test.xml");
//新增
XmlElement _eleParent = _doc.CreateElement("userInfo");
XmlElement _eleId = _doc.CreateElement("user_id");
_eleId.InnerText = "003";
XmlElement _eleName = _doc.CreateElement("Name");
_eleName.InnerText = "李四";
XmlElement _eleQid = _doc.CreateElement("q_id");
_eleQid.InnerText = "0200602070005";
XmlElement _eleQName = _doc.CreateElement("q_Name");
_eleQName.InnerText = "永远幸福";
XmlElement _eleTime = _doc.CreateElement("check_time");
_eleTime.InnerText = "2006-2-17";
_eleParent.AppendChild(_eleId);
_eleParent.AppendChild(_eleName);
_eleParent.AppendChild(_eleQid);
_eleParent.AppendChild(_eleQName);
_eleParent.AppendChild(_eleTime);
_doc.DocumentElement.AppendChild(_eleParent);
_doc.Save(@"new.xml");
1 XmlDocument _doc = new XmlDocument();
2 _doc.Load(@"test.xml");
3
4 //删除
5 XmlNode _node = _doc.SelectSingleNode("//userInfo[user_id='002']");
6 _doc.DocumentElement.RemoveChild(_node);
7
2 _doc.Load(@"test.xml");
3
4 //删除
5 XmlNode _node = _doc.SelectSingleNode("//userInfo[user_id='002']");
6 _doc.DocumentElement.RemoveChild(_node);
7
1 XmlDocument _doc = new XmlDocument();
2 _doc.Load(@"test.xml");
3
4 //更新
5 XmlNode _node = _doc.SelectSingleNode("//userInfo/q_name[../user_id='001']");
6 _node.InnerText = "一家人";
7
8 _doc.Save(@"new.xml");
9
2 _doc.Load(@"test.xml");
3
4 //更新
5 XmlNode _node = _doc.SelectSingleNode("//userInfo/q_name[../user_id='001']");
6 _node.InnerText = "一家人";
7
8 _doc.Save(@"new.xml");
9