Reputation: 37
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
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
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:
match
attribute of node
to the currentPath
and check if the node
has children that are node
s.currentPath
to paths
and returns.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:
Upvotes: 1