下面是我的xml代码:
<?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>我已经编写了一段c#代码来解析每个节点并获得match的值。我不知道如何解析节点来获得所有匹配项的值。在调试代码时,我发现nodelist没有通过"xmlDoc.DocumentElement.SelectNodes("/node");“接收适当的值,这是因为foreach根本不会被执行。我使用"/node“来获取其属性"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++]);
}
}
}
}我需要以字符串的形式查找"match“的值
输出应为:the String = 2:2:19:2
发布于 2016-01-12 20:11:39
最简单的方法是使用递归。
编写一个接受XmlNode node、string currentPath和List<string> paths参数的函数。它应该:
node的match属性附加到currentPath,并检查node是否有子级,如果没有子级,则将currentPath添加到paths并返回。paths.为每个子级调用自身
首先,您将使用根节点、一个空字符串和一个空列表调用此函数。当函数完成时,以前的空列表将包含您的路径,例如您的示例中的两个路径:
发布于 2016-01-12 23:03:58
您可以使用正则表达式来匹配您正在查找的所有属性:
// 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));
}https://stackoverflow.com/questions/34741334
复制相似问题