![](/img/trans.png)
[英]IEnumerable<XElement> is fetching same element more than once
[英]IEnumerable<XElement> compare is not same
我通过以下两种技术读取 XML 文件。
XElement.Parse(File.ReadAllText(xmlfile))
读取整个 XML 注意:我知道我不应该使用这种技术。XDocument.Load(xmlfile);
Load然后我尝试通过以下代码片段创建 XElement 列表。 对我来说,结果看起来相同,但是当我尝试比较两个 IEnumerable 对象时,它们并不相同。
我在俯瞰什么。 这是代码片段
// Read the xml db file.
XElement xEle = XElement.Parse(File.ReadAllText(xmlfile));
XDocument xDoc = XDocument.Load(xmlfile);
List<XElement> xElementCollection = xEle.Elements("Map").ToList();
List<XElement> xDocumentCollection = xDoc.Descendants("Map").ToList();
bool bCompare = xElementCollection.Equals(xDocumentCollection);
bCompare 结果为假,但是当我查看两个列表的数据时。 他们看起来一样。
您基本上需要遍历两个列表中的每个元素,并使用 XNode.DeepEquals 方法按值将它们相互比较。
if (xElementCollection.Count != xDocumentCollection.Count)
{
bCompare = false;
}
else
{
bCompare = true;
for (int x = 0, y = 0;
x < xElementCollection.Count && y < xDocumentCollection.Count; x++, y++)
{
if (!XNode.DeepEquals(xElementCollection[x], xDocumentCollection[y]))
bCompare = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.