user576510
user576510

Reputation: 5915

how to compare XML strings in C#?

In DB I have XML strings stored in a column. Below is my XML structure:

<?xml version="1.0"?>
-<ProductAttributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Attribute Required="false" ID="2" Name="Color">
<Value ID="18">Light Pink</Value>
</Attribute>
-<Attribute Required="false" ID="1" Name="Size">
<Value ID="9">XL</Value>
</Attribute>
</ProductAttributes>

Another XML is:

<?xml version="1.0"?>
-<ProductAttributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Attribute Required="false" ID="1" Name="Size">
<Value ID="1">S</Value>
</Attribute>
-<Attribute Required="false" ID="2" Name="Color">
<Value ID="4">Red</Value>
</Attribute>
-<Attribute Required="false" ID="3" Name="Weight">
<Value ID="6">10gr</Value>
</Attribute>
</ProductAttributes>

Notes

  1. There can be n number of xml strings and each xml string can have m number of tags

  2. Attribute nodes can in different order, for example in 1st attribute Id=1 can be first attribute and in 2nd attribute Id=1 can be last.

Requirement is not compare these n XML strings and find if any of strings has complete duplication of attributes (this comparison will consider values as order can be different).

Please guide and help me.

Upvotes: 0

Views: 1441

Answers (3)

Ryan
Ryan

Reputation: 3982

You could iterate all nodes of xml doc A and for each node, look up its xpath in xml doc B. If any paths are not found or the path is found but the value is different, the doc's are not 'the same'.

You'd then have to do the same for all nodes in B, checking the xpaths in A, to ensure there's nothing "in B but not in A"

Optimise by quitting as 'not equal' as soon as an xpath is not found or the values don't match.

Upvotes: 1

bloudraak
bloudraak

Reputation: 6002

You may want to try The XML Diff and Patch GUI Tool which you can download from here. I've used it before and it works ok.

Upvotes: 1

Dan O
Dan O

Reputation: 6090

don't compare strings of XML. Use them as input to an XML parser that will turn them into XML trees, then search the trees for matching elements and compare their list of attributes.

Upvotes: 2

Related Questions