如何同时比较两个xmlnodeList?c#
我想比较两个xmlNodeList,只有当所有元素和内部文本匹配时才为true,否则为false 这是我的清单的一个例子如何同时比较两个xmlnodeList?c#,c#,xml,linq,C#,Xml,Linq,我想比较两个xmlNodeList,只有当所有元素和内部文本匹配时才为true,否则为false 这是我的清单的一个例子 List 1 Element, Name=\"Color\"" , InnerText = While Element, Name=\"Size\"" , InnerText = small Element, Name=\"Year\"" , InnerText = 2019 List
List 1
Element, Name=\"Color\"" , InnerText = While
Element, Name=\"Size\"" , InnerText = small
Element, Name=\"Year\"" , InnerText = 2019
List 2
Element, Name=\"Color\"" , InnerText = While
Element, Name=\"Size\"" , InnerText = small
Element, Name=\"Year\"" , InnerText = 2018
我现在通过获取列表中的每个元素来比较这两个列表
foreach(xmlNodeList中的XmlNode节点)
{
对于(int i=0;i
问题是,当只有一个元素匹配而不是所有元素匹配时,我得到了正确答案。您应该尝试下面的方法-
var result = false;
foreach (XmlNode node in xmlNodeList)
{
for (int i = 0; i < ComparableAttributes.Count(); i++)
{
//comparison logic is incorrect. you should check the inner xml instead of node directly
//something like node["Color"].InnerText
if (node == Xtemp)
{
result = true;
break; //break the for loop if the result is true
}
else
{
result = false;
}
}
if (!result) break; //break the main foreach if the result is false
}
return result;
你应该试试下面的方法-
var result = false;
foreach (XmlNode node in xmlNodeList)
{
for (int i = 0; i < ComparableAttributes.Count(); i++)
{
//comparison logic is incorrect. you should check the inner xml instead of node directly
//something like node["Color"].InnerText
if (node == Xtemp)
{
result = true;
break; //break the for loop if the result is true
}
else
{
result = false;
}
}
if (!result) break; //break the main foreach if the result is false
}
return result;
因为当
节点==Xtemp
时,您将立即从方法返回。为什么?我不确定当所有节点都相等时如何返回它。你有我可以试试的例子吗?。请务必阅读备注部分!。另请参见,因为当节点==Xtemp
时,您将立即从方法返回。为什么?我不确定当所有节点都相等时如何返回它。你有我可以试试的例子吗?。请务必阅读备注部分!。另请参见=
操作符通过引用而不是通过值来比较两个XmlNode
!非常正确@AlexanderPetrov。我不是在关注比较逻辑,而是在关注他的问题。=
操作符通过引用而不是值来比较两个XmlNode
!非常正确@AlexanderPetrov。我不是在关注比较逻辑,而是在关注他的问题
var xml1 = XDocument.Parse(xmldata1);
var xml2 = XDocument.Parse(xmldata2);
bool result = XNode.DeepEquals(xml1.Document, xml2.Document);