【问题标题】:how to compare XML strings in C#?如何在 C# 中比较 XML 字符串?
【发布时间】:2012-06-30 02:23:50
【问题描述】:

在 DB 中,我将 XML 字符串存储在列中。下面是我的 XML 结构:

<?xml version="1.0"?>
-<ProductAttributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Attribute Required="false" ID="2" Name="Color">
<Value ID="18">Light Pink</Value>
</Attribute>
-<Attribute Required="false" ID="1" Name="Size">
<Value ID="9">XL</Value>
</Attribute>
</ProductAttributes>

另一个 XML 是:

<?xml version="1.0"?>
-<ProductAttributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Attribute Required="false" ID="1" Name="Size">
<Value ID="1">S</Value>
</Attribute>
-<Attribute Required="false" ID="2" Name="Color">
<Value ID="4">Red</Value>
</Attribute>
-<Attribute Required="false" ID="3" Name="Weight">
<Value ID="6">10gr</Value>
</Attribute>
</ProductAttributes>

备注

  1. 可以有n个xml字符串,每个xml字符串可以有m个标签

  2. 属性节点可以有不同的顺序,例如在第一个属性中,Id=1 可以是第一个属性,在第二个属性中,Id=1 可以是最后一个。

要求不是比较这 n 个 XML 字符串,而是查找任何字符串是否具有完全重复的属性(此比较将考虑值,因为顺序可能不同)。

请指导和帮助我。

【问题讨论】:

标签: c# .net xml linq-to-xml


【解决方案1】:

不要比较 XML 字符串。将它们用作 XML 解析器的输入,将它们转换为 XML 树,然后在树中搜索匹配的元素并比较它们的属性列表。

【讨论】:

    【解决方案2】:

    您可以迭代 xml doc A 的所有节点,对于每个节点,在 xml doc B 中查找其 xpath。如果找不到任何路径或找到路径但值不同,则 doc 不'相同'。

    然后您必须对 B 中的所有节点执行相同的操作,检查 A 中的 xpath,以确保没有“B 中但 A 中没有”

    一旦找不到 xpath 或值不匹配,就以“不等于”退出进行优化。

    【讨论】:

      【解决方案3】:

      您可能想尝试The XML Diff and Patch GUI Tool,您可以从here 下载它。我以前用过,效果还可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        • 2021-11-19
        • 2016-02-08
        • 2013-11-22
        • 1970-01-01
        相关资源
        最近更新 更多