SoFunction
Updated on 2025-03-06

Summary of C# method to read xml node data

This article summarizes the method of C# reading xml node data. Share it for your reference. The details are as follows:

The first type:
Using XPath
I configure the XML path under the appSettings node

<appSettings>
  <add key="orgCodePath" value="../../template/home/"/>
</appSettings>

The XML structure is as follows:

<?xml version="1.0" encoding="utf-8" ?> 
<Organizations> 
 <Organization> 
  <ID>1111</ID> 
  <DomainName>aa</DomainName> 
 </Organization> 
 <Organization> 
  <ID>2222</ID> 
  <DomainName>bb</DomainName> 
 </Organization> 
</Organizations>

In C#, I use HashTable for storage:

Hashtable ht = new Hashtable(); 
string orgCodePath = (["orgCodePath"]);
//string orgCodePath = ("../../template/home/");
XmlDocument xmldoc = new XmlDocument();
(orgCodePath);
//Get the node listXmlNodeList topM = ("//Organization"); 
foreach (XmlElement element in topM) 
{ 
 string id = ("ID")[0].InnerText; 
 string domainName = ("DomainName")[0].InnerText; 
 (id, domainName); 
}

The second type:

Read XML through traversal

//Open a file (assuming it is in the root directory)string filename=("/") + @"WebApplication1/"; 
XmlDocument xmldoc= new XmlDocument(); 
(filename); 
//Get the top-level node listXmlNodeList topM=; 
foreach(XmlElement element in topM) 
{ 
   if(()=="appsettings") 
   { 
   //Get the child node of this node   XmlNodeList nodelist=; 
   if (  &gt;0 ) 
   { 
    //(); 
    foreach(XmlElement el in nodelist)//Read element value    { 
    //(["key"].InnerXml); 
    //this.=["key"].InnerText; 
    this.=["key"].Value; 
    this.=["value"].Value; 
      //The element value can be modified here and saved later.     // ["value"].Value=this.; 
    } 
   } 
   } 
} 
(filename); 

Add an element under a node and set the value:

if(()=="appsettings")
{ 
   XmlElement elem =("add");
   (elem); 
   ="ltp"; 
   (filename); 
}

Effect:

&lt;appSettings&gt; 
  &lt;add key="password" value="admin" /&gt; 
  &lt;add&gt;ltp&lt;/add&gt; 
&lt;/appSettings&gt;

Add an element under a node and add two attributes:

if(()=="appsettings") 
{ 
   XmlElement elem =("add"); 
   (elem); 
   XmlAttribute xa=("key"); 
   ="ltp"; 
   XmlAttribute xa2=("value"); 
   ="first"; 
   (xa); 
   (xa2); 
   (filename); 
}

Effect:

&lt;appSettings&gt; 
  &lt;add key="password" value="admin" /&gt; 
  &lt;add key="ltp" value="first" /&gt; 
&lt;/appSettings&gt; 

Add an empty element:

XmlNode node=(groupname); 
=""; 
(node); 
(xmlfile); 

Delete a node element:

string itemname=this.(); 
this.(this.); 
//begin del xmlfile 
XmlDocument doc=new XmlDocument(); 
(xmlfile); 
XmlNodeList topM=; 
foreach(XmlElement element in topM) 
{ 
  if(==this.) 
  { 
   //Get the child node of this node   XmlNodeList nodelist=;    
   foreach(XmlElement el in nodelist)//Read element value   {     
   if(["key"].Value==itemname) 
   { 
    (el); 
   } 
   }//Loop elements  }//Get the group}//Cyclic group(xmlfile); //Be sure to save it, otherwise it won't work//Filter dataprivate void Reader_Xml(string pathFlie) 
{ 
  XmlDocument Xmldoc=new XmlDocument(); 
  (pathFlie); 
  XmlNodeList Record1=(Code[@id='1']);
  int f=0; 
  foreach(XmlNode xnode in Record1) 
  { 
  } 
}

Read xml data Two xml methods

<aaa> 
   <bb>something</bb> 
   <cc>something</cc> 
</aaa> 
<aaa> 
  <add key="123" value="321"/> 
</aaa> 

The first method:

("your xmlfile name"); 
("bb"); 
("cc"); 
("your xmlfile name"); 

The second method:

<aaa>
  <add key="123" value="321"/>
</aaa>

What should I write if I want to find 123 and get 321?

using ; 
XmlDataDocument xmlDoc = new (); 
(@"c:/"); 
XmlElement elem = ("add"); 
string str = ["value"].Value 

The third method: SelectSingleNode  Read xml in two formats:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <appSettings> 
    <ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>        
 </appSettings> 
</configuration> 

XmlDocument doc = new XmlDocument(); 
(strXmlName); 
XmlNode node=("/configuration/appSettings/ConnectionString"); 
if(node!=null) 
{ 
   string k1=;  //null 
   string k2=;//Data Source=yf; user id=ctm_dbo;password=123 
   string k3=;//Data Source=yf; user id=ctm_dbo;password=123 
   node=null; 
}
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <appSettings> 
    <add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />        
 </appSettings> 
</configuration> 

XmlNode node=("/configuration/appSettings/add"); 
if(node!=null)
{ 
   string k=["key"].Value; 
   string v=["value"].Value; 
   node=null; 
} 
XmlNode node=("/configuration/appSettings/add"); 
if(node!=null) 
{
   XmlNodeReader nr=new XmlNodeReader(node); 
   (); 
  //Check whether the current node is a content node.  If this node is not a content node, the reader jumps forward to the next content node or end of the file.   ("value"); 
   string s=; 
   node=null; 
}

I hope this article will be helpful to everyone's C# programming.