This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19874|Reply: 0

[Source] C# reads the XML file node contents

[Copy link]
Posted on 7/11/2015 10:37:33 PM | | |
Suppose the node is:

  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>
Copy code


The code is:

  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.       }  
Copy code






Previous:C# reads XML format files with XMLNS properties
Next:I'm leaving today, and I will explode a few photos of other people's dormitories, quack
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com