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

时间:2022-12-28 20:07:18

目录(?)[-]

  1. XmlDocumentSelectSingleNode方法的使用
  2. XmlDocumentSelectNodes方法的使用
  3. 通过节点属性查找指定节点
 

参考:Select XML Nodes by Name

假设xml文件内容是

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Workflow>
  3. <Activity>
  4. <ActivityId>1</ActivityId>
  5. <ActivityName>start</ActivityName>
  6. <BindingPageId>1</BindingPageId>
  7. <BindingRoleId>1</BindingRoleId>
  8. <ActivityLevel>1</ActivityLevel>
  9. </Activity>
  10. <Activity>
  11. <ActivityId>2</ActivityId>
  12. <ActivityName>pass</ActivityName>
  13. <BindingPageId>2</BindingPageId>
  14. <BindingRoleId>2</BindingRoleId>
  15. <ActivityLevel>2</ActivityLevel>
  16. </Activity>
  17. </Workflow>

<?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

  1. /// <summary>
  2. /// 读取xml中的指定节点的值
  3. /// </summary>
  4. public void ReadXmlNode(string filename)
  5. {
  6. XmlDocument xmlDoc = new XmlDocument();
  7. try
  8. {
  9. xmlDoc.Load(filename);
  10. //读取Activity节点下的数据。SelectSingleNode匹配第一个Activity节点
  11. XmlNode root = xmlDoc.SelectSingleNode("//Activity");//当节点Workflow带有属性是,使用SelectSingleNode无法读取
  12. if (root != null)
  13. {
  14. string ActivityId = (root.SelectSingleNode("ActivityId")).InnerText;
  15. string ActivityName = (root.SelectSingleNode("ActivityName ")).InnerText;
  16. string ActivityLevel = root.SelectSingleNode("ActivityLevel").InnerText;
  17. Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);
  18. }
  19. else
  20. {
  21. Console.WriteLine("the node  is not existed");
  22. //Console.Read();
  23. }
  24. }
  25. catch (Exception e)
  26. {
  27. //显示错误信息
  28. Console.WriteLine(e.Message);
  29. }
  30. }

/// <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);
}
}

然后在主函数中调用该方法

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. XMLOperation xmlOpr = new XMLOperation();
  6. xmlOpr.ReadXmlNode("activity.xml");
  7. Console.Read();
  8. }
  9. }

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方法的使用

  1. /// <summary>
  2. /// 读取xml中的指定节点的值,如果有多个同名节点,则全部读取
  3. /// </summary>
  4. public void ReadXmlNodes(string filename)
  5. {
  6. XmlDocument xmlDoc = new XmlDocument();
  7. try
  8. {
  9. xmlDoc.Load(filename);
  10. XmlNodeList xnList = xmlDoc.SelectNodes("//Activity");
  11. Console.WriteLine("共有{0}个节点", xnList.Count);//输出xnList中节点个数。
  12. foreach (XmlNode xn in xnList)
  13. {
  14. //无法使用xn["ActivityId"].InnerText
  15. string ActivityId = (xn.SelectSingleNode("ActivityId")).InnerText;
  16. string ActivityName = xn.SelectSingleNode("ActivityName").InnerText;
  17. string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;
  18. //  Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);
  19. Console.WriteLine("ActivityId:  {0}/nActivityName:  {1}/nActivityLevel:  {2}", ActivityId, ActivityName, ActivityLevel);
  20. }
  21. }
  22. catch (Exception e)
  23. {
  24. //显示错误信息
  25. Console.WriteLine(e.Message);
  26. }
  27. }

/// <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);
}
}

然后在主函数中调用该方法

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. XMLOperation xmlOpr = new XMLOperation();
  6. xmlOpr.ReadXmlNodes("activity.xml");
  7. Console.Read();
  8. }
  9. }

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/,虽然没有用到参考文章中的方法,不过总觉得以后会用到的。

  1. /// <summary>
  2. ///通过ActivityLevel获取xmlnode,需要保证ActivityLevel在一个xml文档中是唯一的。
  3. /// </summary>
  4. public XmlNode getXmlNode(string filename, string activitylevel)
  5. {
  6. XmlDocument xmlDoc = new XmlDocument();
  7. try
  8. {
  9. xmlDoc.Load(filename);
  10. XmlNodeList xnList = xmlDoc.SelectNodes("//Activity");//当节点Workflow带有属性是,使用SelectSingleNode无法读取
  11. foreach (XmlNode xn in xnList)
  12. {
  13. string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;
  14. if(activitylevel==ActivityLevel)
  15. {
  16. return xn;
  17. }
  18. }
  19. }
  20. catch (Exception e)
  21. {
  22. //显示错误信息
  23. Console.WriteLine(e.Message);
  24. }
  25. return null;
  26. }

/// <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;
}

然后在主函数中调用该方法

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. XMLOperation xmlOpr = new XMLOperation();
  6. XmlNode xn=xmlOpr.getXmlNode("activity.xml", "2");
  7. string ActivityId = (xn.SelectSingleNode("ActivityId")).InnerText;
  8. string ActivityName = (xn.SelectSingleNode("ActivityName ")).InnerText;
  9. string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;
  10. Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);
  11. Console.Read();
  12. }
  13. }

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