user1967685
user1967685

Reputation: 37

how to read from the xml node using c# as per my requirement

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

Upvotes: 0

Views: 228

Answers (2)

TVOHM
TVOHM

Reputation: 2742

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));
}

Upvotes: 0

Alexey
Alexey

Reputation: 1434

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"

Upvotes: 1

Related Questions