我有两个大小相等的XElements列表:
var documentDatabase = XDocument.Parse(xmlDatabase);
var rootElementDatabase = documentDatabase.Root;
var segmentsDatabase = rootElementDatabase.Descendants("Segment");
var documentSlave = XDocument.Parse(xmlSlave);
var rootElementSlave = documentSlave.Root;
var segmentsSlave = rootElementSlave.Descendants("Segment");
基本上是Segment元素的列表,如下所示:
<Segment>
<IdRef>1</IdRef>
<Start>
<Master>0</Master>
<Slave>0</Slave>
<PntType>4</PntType>
</Start>
<End>
<Master>10.059000</Master>
<Slave>29.450302</Slave>
<PntType>4</PntType>
</End>
<Symmetry>0.5</Symmetry>
<FunctionType>1</FunctionType>
</Segment>
我想要的是通过比较<Master>
和<Slave>
标签中的值来比较它们是否相等,忽略其余部分。我怎样才能实现它?
答案 0 :(得分:1)
如果您有多个细分,请创建一个比较它们的方法
Private Function ChkSeg(segment As XElement) As Boolean
If segment.<Start>.<Master>.Value = segment.<End>.<Master>.Value AndAlso
segment.<Start>.<Slave>.Value = segment.<End>.<Slave>.Value Then
Return True
Else
Return False
End If
End Function
然后像这样迭代XML
For Each el As XElement In xe.Elements
If ChkSeg(el) Then
Stop
Else
Stop
End If
Next
使用此
进行测试 Dim xe As XElement
xe = <segments>
<Segment>
<IdRef>1</IdRef>
<Start>
<Master>0</Master>
<Slave>0</Slave>
<PntType>4</PntType>
</Start>
<End>
<Master>10.059000</Master>
<Slave>29.450302</Slave>
<PntType>4</PntType>
</End>
<Symmetry>0.5</Symmetry>
<FunctionType>1</FunctionType>
</Segment>
<Segment>
<IdRef>1</IdRef>
<Start>
<Master>0</Master>
<Slave>0</Slave>
<PntType>4</PntType>
</Start>
<End>
<Master>0</Master>
<Slave>0</Slave>
<PntType>4</PntType>
</End>
<Symmetry>0.5</Symmetry>
<FunctionType>1</FunctionType>
</Segment>
</segments>
答案 1 :(得分:0)
如果要比较开始和结束中的主标签和从属标签,可以使用此方法:
bool CompareXmls(XElement first, XElement second)
{
var firstStart = first.Element("Start");
var firstEnd = first.Element("End");
var firstStartMaster = firstStart.Element("Master").Value;
var firstStartSlave = firstStart.Element("Slave").Value;
var firstEndMaster = firstEnd.Element("Master").Value;
var firstEndSlave = firstEnd.Element("Slave").Value;
var secondStart = second.Element("Start");
var secondEnd = second.Element("End");
var secondStartMaster = secondStart.Element("Master").Value;
var secondStartSlave = secondStart.Element("Slave").Value;
var secondEndMaster = secondEnd.Element("Master").Value;
var secondEndSlave = secondEnd.Element("Slave").Value;
bool areEqual = firstStartMaster == secondStartMaster
&& firstStartSlave == secondStartSlave
&& firstEndMaster == secondEndMaster
&& firstEndSlave == secondEndSlave;
return areEqual;
}
编辑:想要比较已经排序的列表,你可以将一个列表中的第一项与其他列表中的第一项进行比较,依此类推,你可以使用LINQ .Zip方法将XElements分组为一个匿名对象,然后使用LINQ。所有循环项目并进行比较:
var res = firstList.Zip(secondList, (a, b) => new { First = a, Second = b }).All(x=>CompareXmls(x.First,x.Second));
就个人而言,我可能会使用for循环遍历两个列表并比较每个项目,但是如果你想使用单行LINQ,你可以使用上述方法。