0

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

<Kms_Section>ffffff</Kms_section>  
<Kms_Description>bbbb</kms_description>
14
  • 1
    Any reason you need to use the "old" XML API? With LINQ to XML you can just use NextNode... Commented Mar 3, 2015 at 15:26
  • i have many /KMS_doc/KMS_section then kms_section2 Commented Mar 3, 2015 at 15:27
  • That doesn't really answer my question at all... Commented Mar 3, 2015 at 15:27
  • I have an already existing project the old xml api, i want to just to edit something Commented Mar 3, 2015 at 15:28
  • 1
    Are you looking for XmlNode.NextSibling ? Commented Mar 3, 2015 at 15:29

3 Answers 3

1

You could use this code to get to the next 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.
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

The XmlNode object has a property that is called 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. You can loop through them like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.