问题描述
我需要比较两个XmlDocuments以确保它们相同.我想为此任务使用"for each"循环.在下面的代码示例中,我可以遍历aNode(实际)和eNode(预期).但是,我不确定如何 遍历这两个对象,以便使用"for Each"循环进行比较.
***我的伪代码***
对于每个aNode& eNode ...
如果(aNode.FirstChild.ParentNode.Name = eNode.FirstChild.ParentNode.Name和_
aNode.FirstChild.Value = eNode.FirstChild.Value)然后
MsgBox"节点名称&值是相同的!"其他
MsgBox"节点名称&值不相同!"
EndIf
I need to compare two XmlDocuments to make sure they are identical. I am wanting to use a 'for each' loop for this task. In the code example below, I can iterate through both the aNode (actual) and the eNode (expected). However, I'm not certain on how to iterate through these two objects in order to do a compare using a 'for each' loop.
*** My pseudo code ***
For each aNode & eNode...
If (aNode.FirstChild.ParentNode.Name = eNode.FirstChild.ParentNode.Name And _
aNode.FirstChild.Value = eNode.FirstChild.Value) Then
MsgBox "Node names & values are identical!"
Else
MsgBox "Node names & values are NOT identical!"
EndIf
***结束伪代码***
*** End Pseudo Code ***
**代码示例***
将aDoc设为新的XmlDocument()'实际
预期eDoc为新的XmlDocument()'
'阅读实际结果
aDoc.Load("C:\ Temp \ Login_Rs.xml")
'阅读预期结果
eDoc.Load("C:\ Temp \ Login_ExpectedRs.xml")
对于每个aNode作为aDoc.SelectNodes("//*")中的XmlNode
If(Convert.ToString(aNode.FirstChild)<>"")然后
将aFirstchildCopy昏暗为字符串= Convert.ToString(Trim(aNode.FirstChild.Value))
If(aFirstchildCopy<>"")然后,
'打印出所有元素值
Console.WriteLine("aElement Name:" + aNode.FirstChild.ParentNode.Name +,aValue:" + aNode.FirstChild.Value)
如果结束
如果结束
下一个
对于每个eNode作为eDoc.SelectNodes("//*")中的XmlNode
If(Convert.ToString(eNode.FirstChild)<>"")然后
昏暗的eFirstchildCopy为String = Convert.ToString(Trim(eNode.FirstChild.Value))
If(eFirstchildCopy<>"")然后
'打印出所有元素值
Console.WriteLine(" eElement名称:"+ eNode.FirstChild.ParentNode.Name +",eValue:"+ eNode.FirstChild.Value)
如果结束
如果结束
下一个
** Code Example ***
Dim aDoc As New XmlDocument() ' actual
Dim eDoc As New XmlDocument() ' expected
' Read in Actual Results
aDoc.Load("C:\Temp\Login_Rs.xml")
' Read in Expected Results
eDoc.Load("C:\Temp\Login_ExpectedRs.xml")
For Each aNode As XmlNode In aDoc.SelectNodes("//*")
If (Convert.ToString(aNode.FirstChild) <> "") Then
Dim aFirstchildCopy As String = Convert.ToString(Trim(aNode.FirstChild.Value))
If (aFirstchildCopy <> "") Then
' Print out all the element values
Console.WriteLine("aElement Name: " + aNode.FirstChild.ParentNode.Name + ", aValue: " + aNode.FirstChild.Value)
End If
End If
Next
For Each eNode As XmlNode In eDoc.SelectNodes("//*")
If (Convert.ToString(eNode.FirstChild) <> "") Then
Dim eFirstchildCopy As String = Convert.ToString(Trim(eNode.FirstChild.Value))
If (eFirstchildCopy <> "") Then
' Print out all the element values
Console.WriteLine("eElement Name: " + eNode.FirstChild.ParentNode.Name + ", eValue: " + eNode.FirstChild.Value)
End If
End If
Next
***结束代码示例***
*** End Code Example ***
推荐答案
如果您仍然想创建自己的比较例程,请告诉我们您要在文档中进行比较的内容(即元素名称,元素内容等),我们可以提供更多帮助.我建议您设置一个递归例程,以深入了解 每个节点的子节点.
If you still want to create your own comparison routine, let us know what you want to compare within the document (i.e. element names, element content, etc) and we can help a bit more. I would suggest setting up a recursive routine that can drill into the child nodes of each node.
这篇关于在两个元素上使用For Each的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!