c# – 获取输出diffgram中的节点名称而不是其索引

我正在使用
XMLdiffpatch工具来检测两个xml文件之间的更改,该工具的输出xml文件如下所示:

<?xml version="1.0" encoding="utf-16"?>
<xd:xmldiff version="1.0" srcDocHash="5708212576896487287" options="None" fragments="no" xmlns:xd="http://www.microsoft.com/xmldiff">
    <xd:node match="2">
        <xd:node match="3"/>
        <xd:add>
            <e>Some text 4</e>
            <f>Some text 5</f>
        </xd:add>
        <xd:node match="4">
            <xd:change match="1">Changed text</xd:change>
            <xd:remove match="2"/>
        </xd:node>
        <xd:node match="5">
            <xd:remove match="@secondAttr"/>
            <xd:add type="2" name="newAttr">new value</xd:add>
            <xd:change match="@firstAttr">changed attribute value</xd:change>
        </xd:node>
        <xd:remove match="6" opid="1"/>
        <xd:add type="1" name="p">
            <xd:add type="1" name="q">
                <xd:add match="/2/6" opid="1"/>
            </xd:add>
        </xd:add>
    </xd:node>
    <xd:descriptor opid="1" type="move"/>
</xd:xmldiff>

第一档:

<?xml version="1.0"?>
<b>
  <a>Some text 1</a>
  <b>Some text 2</b>
  <c>Some text 3</c>
  <d>
    Another text
    <foo/>
  </d>
  <x firstAttr="value1" secondAttr="value2"/>
  <y>
    <!--Any comments?-->
    <z id="10">Just another text</z>
  </y>
</b>

第二档:

<?xml version="1.0"?>
<b>
  <a>Some text 1</a>
  <b>Some text 2</b>
  <c>Some text 3</c>
  <e>Some text 4</e>
  <f>Some text 5</f>
  <d>Changed text</d>
  <x firstAttr="changed attribute value" newAttr="new value"/>
  <p>
    <q>
      <y>
        <!--Any comments?-->
        <z id="10">Just another text</z>
      </y>
    </q>
  </p>
</b>

如您所见,xml在其索引对应于其父节点时显示检测到的节点更改.
我现在面临的问题是如何解析这些索引,以便用原始xml文件中的实际节点名替换它们.

最佳答案 您在xml diff中看到的“匹配”数字是子节点的相对索引.构建整个xml diff以从第一个文件构造第二个文件.在你的例子中,

<xd:node match="2">
  <xd:node match="3"/>
    <xd:add>
        <e>Some text 4</e>
        <f>Some text 5</f>
    </xd:add>

意思是:

“在第一个文件中,从根目录中找到第二个子节点” – 即节点
&LT b取代;在<?xml>之后宣言.

“在找到的节点中,找到第三个子节点” – 即< c>某些文本3< / c>

“找到节点后,插入以下文字” – 插入节点e和f.

在MSDN上有一篇关于xmldiff format的详细文章,有一些代码示例和xmldiff语言规范.

因此,为了用真实值替换索引,您需要根据diff索引遍历源文档,并提取实际节点名称. This question有很好的代码示例来遍历子节点.

点赞