0

Here is my xml code:

  <?xml version="1.0" encoding="utf-8"?>
<xd:xmldiff version="1.0" srcDocHash="11928043053884448382" options="IgnoreChildOrder IgnoreNamespaces IgnoreWhitespace IgnoreXmlDecl " fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
    <xd:node match="2">
        <xd:node match="2">
            <xd:node match="19">
                <xd:node match="2">
                    <xd:add>Y</xd:add>
                </xd:node>
            </xd:node>
            <xd:add match="/2/2/11" opid="2" />
            <xd:change match="18" name="OWNINGSITE">
                <xd:node match="2">
                    <xd:remove match="1" />
                </xd:node>
            </xd:change>
            <xd:add match="/2/2/2-9" opid="1" />
            <xd:change match="17" name="STATUS">
                <xd:node match="2">
                    <xd:remove match="1" />
                </xd:node>
            </xd:change>
            <xd:remove match="14-16" />
            <xd:remove match="13" subtree="no">
                <xd:remove match="1-2" />
            </xd:remove>
            <xd:remove match="11" opid="2" />
            <xd:remove match="10" />
            <xd:remove match="2-9" opid="1" />
            <xd:remove match="1" />
        </xd:node>
        <xd:node match="5">
            <xd:node match="3">
                <xd:node match="11">
                    <xd:change match="1">0,1,0,1,0,0,0,0,1</xd:change>
                </xd:node>
            </xd:node>
        </xd:node>
    </xd:node>
    <xd:descriptor opid="1" type="move" />
    <xd:descriptor opid="2" type="move" />
</xd:xmldiff>

I have written a c# code to parse through each node and get the value of match. I am unable to figure out how to parse through the nodes to get the values of all the matches. While debugging the code I found out that nodelist is not recieving the appropriate value through "xmlDoc.DocumentElement.SelectNodes("/node");" due to which foreach is not getting executed at all. Is my usage of "/node" right to get the values of its attribute "match"?

namespace demo
{
    class Program
    {
        static void Main()
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("C:\\shreyas\\NX_Temp\\NX_Temp\\000048_A\\CompareReport3D.xml");
            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/node");
            string[] arr1 = new string[10];
            int i = 0;

            foreach (XmlNode node in nodeList)
            {
                 arr1[i] = node.Attributes["match"].Value;
                 i++;
            }

            i = 0;
            while (arr1[i] != null)
            {
                Console.WriteLine("the String=" + arr1[i]+ ":" + arr1[i++]+ ":" + arr1[i++]+":" + arr1[i++]);
            }

        }
    }
}

I need to find the values of "match" in the form of string

output should be: the String = 2:2:19:2

12
  • Please provide a more detailed sample output for your example. I cannot understand what exactly the result of your program should be. Commented Jan 12, 2016 at 11:43
  • Hi, I have made few changes to the code and added the sample output Commented Jan 12, 2016 at 11:50
  • So, should it output the path made of "match" values for each node that has no children? Commented Jan 12, 2016 at 11:55
  • yeah it should get match values Commented Jan 12, 2016 at 11:56
  • 1
    While debugging the code I found out that nodelist is not recieving the appropriate value throught "xmlDoc.DocumentElement.SelectNodes("/node");" due to which foreach is not getting executed at all. Commented Jan 12, 2016 at 14:35

2 Answers 2

1

The easiest way would be to use recursion.

Write a function that takes an XmlNode node, a string currentPath and a List<string> paths. It should:

  1. append the match attribute of node to the currentPath and check if the node has children that are nodes.
  2. If there's no children, it adds currentPath to paths and returns.
  3. If there are children, it calls itself for each child by passing child, currentPath (which has been modified) and paths.

To start, you will call this function with the root node, an empty string and an empty list. When the function completes, the formerly empty list will contain your paths, e.g. two paths for your example:

  • "2:2:19:2"
  • "2:5:3:11"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alexey :) but I am unable to understand your answer
0

You could use Regex to match all the attributes you are looking for:

// Find all attributes that match the pattern match="x" - where x can be any value.
foreach (var match in Regex.Matches(str, "match=\"([^\"]+)\""))
{
    // For each match, match only the "x" part of the result and remove the containing ".
    Console.WriteLine(Regex.Match(match.ToString(), "\"([^\"]+)\"").ToString()
        .Replace("\"", string.Empty));
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.