标签:each div == xml文件 reac foreach append x11 set
//读取
XmlDocument doc =
new
XmlDocument();
doc.Load(Server.MapPath(
"person.xml"
));
//加载Xml文件
XmlElement root = doc.DocumentElement;
//获取根节点
XmlNodeList personNodes = root.GetElementsByTagName(
"Person"
);
//获取Person子节点集合
foreach
(XmlNode node
in
personNodes)
{
string
id = ((XmlElement)node).GetAttribute(
"id"
);
//获取Name属性值
string
name = ((XmlElement)node).GetElementsByTagName(
"Name"
)[0].InnerText;
//获取Age子XmlElement集合
string
age = ((XmlElement)node).GetElementsByTagName(
"Age"
)[0].InnerText;
Console.WriteLine(
"编号:"
+ id +
"姓名:"
+ name +
"年龄:"
+ age);
}
//添加
XmlDocument doc =
new
XmlDocument();
doc.Load(Server.MapPath(
"person.xml"
));
XmlElement root = doc.DocumentElement;
//根节点的添加独立子节点
XmlElement person = doc.CreateElement(
"Person"
);
person.SetAttribute(
"id"
,
"3"
);
person.AppendChild(getChildNode(doc,
"Name"
,
"Elephant"
));
person.AppendChild(getChildNode(doc,
"Age"
,
"23"
));
root.AppendChild(person);
doc.Save(Server.MapPath(
"person.xml"
));
Console.WriteLine(
"XML文件节点添加成功"
);
//编辑
XmlDocument doc =
new
XmlDocument();
doc.Load(Server.MapPath(
"person.xml"
));
//加载Xml文件
XmlElement root = doc.DocumentElement;
//获取根节点
XmlNodeList personNodes = root.GetElementsByTagName(
"Person"
);
//获取Person子节点集合
foreach
(XmlNode node
in
personNodes)
{
XmlElement ele = (XmlElement)node;
if
(ele.GetAttribute(
"id"
) ==
"2"
)
{
XmlElement nameEle = (XmlElement)ele.GetElementsByTagName(
"Name"
)[0];
nameEle.InnerText = nameEle.InnerText +
"修改"
;
}
}
Console.WriteLine(
"节点修改成功"
);
doc.Save(Server.MapPath(
"person.xml"
));
//删除
XmlDocument doc =
new
XmlDocument();
doc.Load(Server.MapPath(
"person.xml"
));
//加载Xml文件
XmlElement root = doc.DocumentElement;
//获取根节点
XmlNodeList personNodes = root.GetElementsByTagName(
"Person"
);
//获取Person子节点集合
XmlNode selectNode =root.SelectSingleNode(
"/Persons/Person[@id=‘1‘]"
);
root.RemoveChild(selectNode);
Console.WriteLine(
"节点删除成功"
);
doc.Save(Server.MapPath(
"person.xml"
));
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("/xmlconfig/cunzhengfee.xml"));
XmlElement root = doc.DocumentElement;
//根节点的添加独立子节点
XmlElement person = doc.CreateElement("cunzhengfee");
person.InnerText = "10000";
root.AppendChild(person);
doc.Save(Server.MapPath("/xmlconfig/cunzhengfee.xml"));
Console.WriteLine("XML文件节点添加成功");
修改:
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("/xmlconfig/cunzhengfee.xml")); //加载Xml文件
XmlElement root = doc.DocumentElement; //获取根节点
XmlNode personNode = root.GetElementsByTagName("cunzhengfee")[0]; //获取cunzhengfee
personNode.InnerText = "12000";
doc.Save(Server.MapPath("/xmlconfig/cunzhengfee.xml"));
标签:each div == xml文件 reac foreach append x11 set
原文地址:https://www.cnblogs.com/sophiel/p/9020126.html