简体   繁体   English

读取C#Xml文件

[英]C# Xml file read

How to go to brother node in xml file, i want to go step back to parent node and then go step forward to brother node 如何转到xml文件中的兄弟节点,我想退回到父节点,然后再前进到兄弟节点

<Kms_Section>ffffff</Kms_section>  
<Kms_Description>bbbb</kms_description>

You could use this code to get to the next kms_section2: 您可以使用以下代码进入下一个kms_section2:

XmlNode FoundNode = null;
while (node.NextSibling != null && FoundNode == null)
{
    node = node.NextSibling;
    if (node.Name == "kms_section2")
    {
         FoundNode = node;
    }
}
if (FoundNode != null)
{
    //Do whatever you want.
}

The XmlNode object has a property that is called NextSibling. XmlNode对象具有一个称为NextSibling的属性。 It is the next node under the parent node for the specified node. 它是指定节点的父节点下的下一个节点。 But I think you just want the next node in the XmlNodeList. 但是我认为您只需要XmlNodeList中的下一个节点。 You can loop through them like this: 您可以像这样遍历它们:

foreach (XmlNode node in nodes)
{
  //Do whatever you want.
} 

I think you can use my earlier example. 我认为您可以使用我之前的示例。 Instead of doing 而不是做

XmlNodeList nodes =  doc.DocumentElement.SelectNodes("/KMS_doc/KMS_section");

you do 你做

XmlNode parent = doc.DocumentElement.SelectSingleNode("KMS_doc");

and then you go with 然后你去

XmlNodeList nodes = parent.SelectNodes("KMS_section"); 

you process all elements inside of nodes and then you go with 您处理节点内的所有元素,然后

nodes = parent.SelectNodes("KMS_dataSection"); 

and process those elements. 并处理这些元素。

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

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