<root>
<aa>
<bbb num="2">
<aaa num="1">
</aa>
<bb>
<bbb num="2">
<aaa num="3">
<bbb num="1">
<aaa num="4">
</bb>
<cc>
<bbb num="1">
<aaa num="3">
<bbb num="2">
<aaa num="4">
</cc>
</root>
取第三层num值为1的节点
<aa>
<bbb num="2">
<aaa num="1">
</aa>
<bb>
<bbb num="2">
<aaa num="3">
<bbb num="1">
<aaa num="4">
</bb>
<cc>
<bbb num="1">
<aaa num="3">
<bbb num="2">
<aaa num="4">
</cc>
</root>
取第三层num值为1的节点
XmlDocument doc = new XmlDocument();
doc.Load("......");
string xpath = "/*/*/*[@num='1']";
XmlNodeList xnl = doc.DocumentElement.SelectNodes(xpath);
Debug.WriteLine(xnl.Count);
<aa>
<bbb num="11">
<aaa num="1">
</aa>
<bb>
<bbb1 num="2">
<aaa1 num="12">
<bbb2 num="5">
<aaa2 num="4">
</bb>
<cc>
<bbb3 num="6">
<aaa3 num="3">
<bbb4 num="7">
<aaa4 num="8">
</cc>
</root>
取num为1, 2, 3的节点。
<root>
<aa>
<bbb num="11"/>
<aaa num="1"/>
</aa>
<bb>
<bbb1 num="2"/>
<aaa1 num="12"/>
<bbb2 num="5"/>
<aaa2 num="4"/>
</bb>
<cc>
<bbb3 num="6"/>
<aaa3 num="3"/>
<bbb4 num="7"/>
<aaa4 num="8"/>
</cc>
</root>
XmlDocument doc = new XmlDocument();
doc.Load("......");
string xpath = "/*/*/*[@num='1' or @num='2' or @num='3']";
XmlNodeList xnl = doc.DocumentElement.SelectNodes(xpath);
Debug.WriteLine(xnl.Count);
using System.Data;
using System.Configuration;
using System.Xml;
namespace form
{
/// <summary>
/// ParseXml 的摘要说明
/// </summary>
///
public class ParseXml
{
XmlDocument doc;
private string FilePath;
public ParseXml()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public ParseXml(string filepath)
{
doc = new XmlDocument();
FilePath = filepath;
doc.Load(filepath); }
public string parse(string XPath)
{
XmlNode x1 = doc.GetElementsByTagName(XPath).Item(0);
return x1.InnerText;
} public void update(string XPath, string content)
{
XmlNode x1 = doc.GetElementsByTagName(XPath).Item(0);
x1.InnerText = content;
doc.Save(FilePath);
}
public void close()
{
doc = null;
}
public string bianli() {
XmlNodeList nodeList = doc.SelectSingleNode("contact_us").ChildNodes;
XmlElement xe = null;
string s = "";
foreach(XmlNode xn in nodeList)
{
xe = (XmlElement)xn;
s+=xe.InnerText;
}
return s;
}
}
}