Skip to content

Instantly share code, notes, and snippets.

@crossan007
Forked from ctigeek/Compare-XmlDocs.ps1
Created December 30, 2019 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crossan007/0f4ef3e30f18c23f7d728261033400d1 to your computer and use it in GitHub Desktop.
Save crossan007/0f4ef3e30f18c23f7d728261033400d1 to your computer and use it in GitHub Desktop.
Powershell - Compare two XML documents.
function Compare-XmlDocs($actual, $expected) {
if ($actual.Name -ne $expected.Name) {
throw "Actual name not same as expected: actual=" + $actual.Name
}
##attributes...
if ($actual.Attributes.Count -ne $expected.Attributes.Count) {
throw "attribute mismatch for actual=" + $actual.Name
}
for ($i=0;$i -lt $expected.Attributes.Count; $i =$i+1) {
if ($expected.Attributes[$i].Name -ne $actual.Attributes[$i].Name) {
throw "attribute name mismatch for actual=" + $actual.Name
}
if ($expected.Attributes[$i].Value -ne $actual.Attributes[$i].Value) {
throw "attribute value mismatch for actual=" + $actual.Name
}
}
##children
if ($expected.ChildNodes.Count -ne $actual.ChildNodes.Count) {
throw "child node mismatch. for actual=" + $actual.Name
}
for ($i=0;$i -lt $expected.ChildNodes.Count; $i =$i+1) {
if (-not $actual.ChildNodes[$i]) {
throw "actual missing child nodes. for actual=" + $actual.Name
}
Compare-XmlDocs $actual.ChildNodes[$i] $expected.ChildNodes[$i]
}
if ($expected.InnerText) {
if ($expected.InnerText -ne $actual.InnerText) {
throw "inner text mismatch for actual=" + $actual.Name
}
}
elseif ($actual.InnerText) {
throw "actual has inner text but expected does not for actual=" + $actual.Name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment