简体   繁体   English

读取XMLDocument节点,而不用C#读取其子节点

[英]Read XMLDocument node without reading its child nodes in C#

Sample XML 样本XML

<A>
   <B>
      <B1/>

      <B2/>
      <B3/>
      <B4/>
      <B5/>
   </B>
   <C>
      <C1/>
      <C2/>
      <C3/>
      <C4/>
      <C5/>
   </C>
</A>

Query: C# When I read the the child nodes of A it retuns nodes B & C with their child nodes. 查询: C#当我读取A的子节点时,它将使用其子节点重新调整B和C节点。

Is there any possibility so that I can get only B & C without their respective child nodes 有没有可能让我只得到B&C而没有他们各自的子节点

I need to populate the tree with this type of xml & the xml file is quite big. 我需要用这种xml类型填充树,并且xml文件很大。 so I need to load the childs at the time of expanding any node 所以我需要在扩展任何节点时加载子节点

Requirement is Suppose I try to expand A node the I want only B & C, 要求是假设我尝试扩展一个我只想要B&C的节点,

If I expand B then I want B1 to B5 如果我将B展开,那么我希望B1到B5

Use a XmlReader. 使用XmlReader。 XmlDocument by design has to load the whole Xml document into memory. 根据设计,XmlDocument必须将整个Xml文档加载到内存中。

如果使用Java,则可以实现SAX处理程序,以构建DOM并忽略子级。

It's a badly worded question so I'm not entirely sure what you are trying to do but if you just want all the child nodes of the root (A) then use an XmlDocument with XPath like this: 这是一个措辞不好的问题,所以我不能完全确定您要做什么,但是如果您只想要根(A)的所有子节点,则可以将XmlDocument与XPath一起使用,如下所示:

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList nodes = doc.SelectNodes("/A/*");
foreach(XmlNode node in nodes){
   //DO STUFF
}

if i understand the question right, u need to get children of the node without getting their children. 如果我理解正确的问题,那么您需要在不获取其子节点的情况下获取该节点的子节点。 this can be done by xquery (child::*) 这可以通过xquery(child :: *)完成

so if u apply it in A node it will give B and C. if u apply it in B then it will give B1-B5. 因此,如果您将其应用于A节点,它将给出B和C。如果您将其应用于B,则它将给出B1-B5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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