C# Xml 操作
//读取
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode XNSpec = xmlDoc.SelectSingleNode("spec");
XmlNodeList XLlist = XNSpec.ChildNodes; //读取所有节点
foreach (XmlNode xnnode in XLlist)
{
if (xnnode.Name.ToLower() == specname)
{
returnStr = xnnode.InnerText.Trim();
}
}
// 插入
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode XNSpec = xmlDoc.SelectSingleNode("spec");
XmlNodeList XLlist = XNSpec.ChildNodes;
foreach (XmlNode xnnode in XLlist)
{
if (xnnode.Name.ToLower() == lowspecname)
{
xnnode.InnerText = specvalue;
hasSetted = true;
}
}
// 若没有设定过则需要添加字段
if (!hasSetted)
{
XmlElement elem = xmlDoc.CreateElement(specname);
elem.InnerText = specvalue;
XNSpec.AppendChild(elem);
}