假設xml文件內容是
- <?xml version="1.0" encoding="utf-8"?>
- <Workflow>
- <Activity>
- <ActivityId>1</ActivityId>
- <ActivityName>start</ActivityName>
- <BindingPageId>1</BindingPageId>
- <BindingRoleId>1</BindingRoleId>
- <ActivityLevel>1</ActivityLevel>
- </Activity>
- <Activity>
- <ActivityId>2</ActivityId>
- <ActivityName>pass</ActivityName>
- <BindingPageId>2</BindingPageId>
- <BindingRoleId>2</BindingRoleId>
- <ActivityLevel>2</ActivityLevel>
- </Activity>
- </Workflow>
我們需要讀取Activity節點下的內容,因為存在多個Activity,所以需要讀取多個節點。可以使用 XmlDocument.SelectSingleNode和XmlDocument.SelectNodes方法查找節點。前一個是查找匹配的第一個節點,而后一個則會返回一個節點列表。
1.XmlDocument.SelectSingleNode方法的使用
首先創建一個讀取xml文件的方法ReadXmlNode
- /// <summary>
- /// 讀取xml中的指定節點的值
- /// </summary>
- public void ReadXmlNode(string filename)
- {
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(filename);
- //讀取Activity節點下的數據。SelectSingleNode匹配第一個Activity節點
- XmlNode root = xmlDoc.SelectSingleNode("//Activity");//當節點Workflow帶有屬性是,使用SelectSingleNode無法讀取
- if (root != null)
- {
- string ActivityId = (root.SelectSingleNode("ActivityId")).InnerText;
- string ActivityName = (root.SelectSingleNode("ActivityName ")).InnerText;
- string ActivityLevel = root.SelectSingleNode("ActivityLevel").InnerText;
- Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);
- }
- else
- {
- Console.WriteLine("the node is not existed");
- //Console.Read();
- }
- }
- catch (Exception e)
- {
- //顯示錯誤信息
- Console.WriteLine(e.Message);
- }
- }
然后在主函數中調用該方法
- class Program
- {
- static void Main(string[] args)
- {
- XMLOperation xmlOpr = new XMLOperation();
- xmlOpr.ReadXmlNode("activity.xml");
- Console.Read();
- }
- }
輸出結果為:
ActivityId:1 ActivityName:start ActivityLevel:1
2.XmlDocument.SelectNodes方法的使用
- /// <summary>
- /// 讀取xml中的指定節點的值,如果有多個同名節點,則全部讀取
- /// </summary>
- public void ReadXmlNodes(string filename)
- {
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(filename);
- XmlNodeList xnList = xmlDoc.SelectNodes("//Activity");
- Console.WriteLine("共有{0}個節點", xnList.Count);//輸出xnList中節點個數。
- foreach (XmlNode xn in xnList)
- {
- //無法使用xn["ActivityId"].InnerText
- string ActivityId = (xn.SelectSingleNode("ActivityId")).InnerText;
- string ActivityName = xn.SelectSingleNode("ActivityName").InnerText;
- string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;
- // Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);
- Console.WriteLine("ActivityId: {0}/nActivityName: {1}/nActivityLevel: {2}", ActivityId, ActivityName, ActivityLevel);
- }
- }
- catch (Exception e)
- {
- //顯示錯誤信息
- Console.WriteLine(e.Message);
- }
- }
然后在主函數中調用該方法
- class Program
- {
- static void Main(string[] args)
- {
- XMLOperation xmlOpr = new XMLOperation();
- xmlOpr.ReadXmlNodes("activity.xml");
- Console.Read();
- }
- }
輸出結果為:
共有2個節點 ActivityId: 1 ActivityName: start ActivityLevel: 1 ActivityId: 2 ActivityName: pass ActivityLevel: 2
3.通過節點屬性查找指定節點
參考http://www.csharp-examples.net/xml-nodes-by-attribute-value/,雖然沒有用到參考文章中的方法,不過總覺得以后會用到的。
- /// <summary>
- ///通過ActivityLevel獲取xmlnode,需要保證ActivityLevel在一個xml文檔中是唯一的。
- /// </summary>
- public XmlNode getXmlNode(string filename, string activitylevel)
- {
- XmlDocument xmlDoc = new XmlDocument();
- try
- {
- xmlDoc.Load(filename);
- XmlNodeList xnList = xmlDoc.SelectNodes("//Activity");//當節點Workflow帶有屬性是,使用SelectSingleNode無法讀取
- foreach (XmlNode xn in xnList)
- {
- string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;
- if(activitylevel==ActivityLevel)
- {
- return xn;
- }
- }
- }
- catch (Exception e)
- {
- //顯示錯誤信息
- Console.WriteLine(e.Message);
- }
- return null;
- }
然后在主函數中調用該方法
- class Program
- {
- static void Main(string[] args)
- {
- XMLOperation xmlOpr = new XMLOperation();
- XmlNode xn=xmlOpr.getXmlNode("activity.xml", "2");
- string ActivityId = (xn.SelectSingleNode("ActivityId")).InnerText;
- string ActivityName = (xn.SelectSingleNode("ActivityName ")).InnerText;
- string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;
- Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);
- Console.Read();
- }
- }
輸出結果為:
ActivityId:2 ActivityName:pass ActivityLevel:2