C#中读取xml文件指定节点

参考:Select XML Nodes by Name

假设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

你可能感兴趣的:(xml,exception,workflow,String,C#,Class)