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

user1967685
  • 27
  • 2
  • 9
  • Please provide a more detailed sample output for your example. I cannot understand what exactly the result of your program should be. – Alexey Jan 12 '16 at 11:43
  • Hi, I have made few changes to the code and added the sample output – user1967685 Jan 12 '16 at 11:50
  • So, should it output the path made of "match" values for each node that has no children? – Alexey Jan 12 '16 at 11:55
  • yeah it should get match values – user1967685 Jan 12 '16 at 11:56
  • Do you get an error or is the output false? I see some strange things in the while loop, but I'm not sure what your precise problem is – Kyra Jan 12 '16 at 12:55
  • The output i mentioned is the output i want to get. Right now I am getting an error – user1967685 Jan 12 '16 at 13:12
  • What kind of an error? – Kyra Jan 12 '16 at 14:17
  • 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. – user1967685 Jan 12 '16 at 14:35
  • Possible duplicate of [C# XmlDocument SelectNodes is not working](http://stackoverflow.com/questions/16889895/c-sharp-xmldocument-selectnodes-is-not-working) – Kyra Jan 12 '16 at 14:39
  • But here I want to know whether the usage of "/node" in xmlDoc.DocumentElement.SelectNodes("/node"); is right to get the values of its attribute "match" – user1967685 Jan 12 '16 at 14:49
  • Maybe you need to do `xmlDoc.DocumentElement.SelectNodes("/xd:node")`. I also kind of get the feeling that this isn't the entire XML, because I miss the first line. But in the basis it is the same question, that's why I marked it as a duplicate – Kyra Jan 12 '16 at 15:00
  • i added the entire xml now – user1967685 Jan 12 '16 at 15:03

2 Answers2

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"
Alexey
  • 1,354
  • 13
  • 30
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));
}
TVOHM
  • 2,740
  • 1
  • 19
  • 29