简体   繁体   中英

how to get the nodes which contain another xml nodes in c#

I have few xml documents. and each document has different nodes. Only thing common is it has _Header and _Table nodes. what i want is to get the parent nodes which contain _Header and _Table nodes.

在此处输入图片说明

I want the program to output the following nodes:

_StatementofNetAssets_T1
_StatementofNetAssets_T2
_StatementofNetAssets_T3

How can I do this?

XmlDocument xmlDoc=new XmlDocument(); 
string xmlname=Server.MapPath("*.xml").ToString();
xmlDoc.Load(xmlname); 
XmlNodeList    nodeList=xmlDoc.SelectSingleNode("form/Documents").ChildNodes;//get all child nodes
foreach(XmlNode xn in nodeList) 
{ 
  XmlElement xe2=(XmlElement)xn;

  if(xe2.InnerText=="_Header"||xe2.InnerText=="_Table nodes")
  {
    XmlNode xn3=xn.ParentNode; 
    XmlElement xe=(XmlElement)xn3; 
    Console.WriteLine(xe.InnerText);
   }
} 

You could use XDocument class and XPath to quickly parse your documents

var elements = XDocument.Load(path).XPathSelectElements("//_Header").Select(q => q.Parent);

You will be needing these namespaces

using System.Linq;
using System.Xml.XPath;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM