【发布时间】:2015-04-05 17:12:12
【问题描述】:
我的问题很简单。我正在使用 XPath 从 xml 文件中检索信息。我的目标是比较 2 个 XML 文件,它们应该是相同的。我对“单个”节点没有任何问题。当有兄弟姐妹时,我的问题就来了。
看起来是这样的:
XPathDocument expectedDocument = new XPathDocument("C:\\expected.xml");
XPathDocument testedDocument = new XPathDocument("C:\\tested.xml");
XPathNavigator expectedNav = expectedDocument.CreateNavigator();
XPathNavigator testedNav = testedDocument.CreateNavigator();
XPathNodeIterator expectedIterator;
XPathNodeIterator testedIterator;
string expectedStr;
string testedStr;
string parameter;
parameter = "/DonneesDepot/Identification/@CoclicoFacturation";
expectedStr = expectedNav.SelectSingleNode(parameter).Value;
testedStr = testedNav.SelectSingleNode(parameter).Value;
CompareValues(expectedStr, testedStr, parameter);
效果很好。现在变得复杂的是这种 XML:
<Surtaxe> <Zone CodeZoneSurtaxe="1" NbPlisZone="0" PoidsZone="0" /> <Zone CodeZoneSurtaxe="2" NbPlisZone="2" PoidsZone="2" /> </Surtaxe>
我希望能够确保两个文件中“附加税”的内容相同(记住顺序并不重要),所以我尝试了这个:
parameter = "/DonneesDepot/Facturation/Surtaxe/Zone/@PoidsZone";
expectedIterator = expectedNav.Select(parameter);
testedIterator = testedNav.Select(parameter);
while (expectedIterator.MoveNext() && testedIterator.MoveNext())
{
CompareValues(expectedIterator.Current.Value, testedIterator.Current.Value, parameter);
}
但是即使 XML 都包含这两行,它们有时也不在相同的顺序,所以我的 while 循环不起作用。
比较以下两个最容易的是什么(这种比较的预期结果是“平等”)
<Surtaxe>
<Zone CodeZoneSurtaxe="1" NbPlisZone="1" PoidsZone="1" />
<Zone CodeZoneSurtaxe="2" NbPlisZone="0" PoidsZone="0" />
</Surtaxe>
和
<Surtaxe>
<Zone CodeZoneSurtaxe="2" NbPlisZone="0" PoidsZone="0" />
<Zone CodeZoneSurtaxe="1" NbPlisZone="1" PoidsZone="1" />
</Surtaxe>
谢谢
【问题讨论】:
-
您的问题并不“简单”,而且指定得很差。你不能指望程序员根据这个问题的“规范”编写代码,它主要基于一个输入和输出的例子。