xml学习笔记!(c#)
初学xml,边学边作点笔记吧!
1.选中特定结点的方法:
XmlDocument myDoc = new XmlDocument();
myDoc.Load(xpath);
message.Text=myDoc.SelectSingleNode ("//person[name='Tim Daly']").ChildNodes.Item(2).InnerText;}
//说明:SelectSingleNode选中某个Node ,()里是给Node 的Id,“//”表示是任意层的结点。
//可以XmlNode root=myDoc.SelectSingleNode("books"); books is the root's id!
//ChildNodes是所有下一层结点. name title title2
//<person>
// <name>Tim Daly</name>
// <title>CTO</title>
// <title2>CTO0</title2> ----> It is the “Item(2)“;
// </person>
2.如何得到特定名称的所有结点?
XmlDocument myDoc = new XmlDocument();
myDoc.Load(xpath);
XmlNodeList list=myDoc.GetGetElementsByTagName("name");// 这样就可以得到所有"name"元素
//操作如下:
for (int i=0; i < list.Count; i++)
{
list[i].InnerXml;
}
(待续.....)