I am getting responses from two different web services in XML format. Both web services have the same logic but developed in different technologies. We are shifting our web services to Microsoft Technology. The web service engine is the core which is connected to many other application and providing different services to them.
Whenever there is a call to a production web service, we pass a similar call to the web service which is developed on Microsoft Technologies and save both responses in separate folders.
Now, we have to compare both responses (XML). There are lot of sorting and intending issues. I would like to avoid all the sorting and indenting issues so I can get the right comparison report.
Is there a way you can sort and indent
XML before saving (XMLDocument.Save) it?
Thanks.
Solution:
I have found some XSLT that does that on the net, but there seems to be a
problem when the elements have attributes.
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-template select="@* | node()">
<xsl:sort select="name()"/>
</xsl:apply-template>
</xsl:copy>
</xsl:template>
Indeed, attribute nodes have to be copied to the result tree before any nodes of another type. Because of the sorting, the node-set loses document order and thus cannot any longer guarantee that the attributes are processed earlier than the elements and text nodes.
one solution is this:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*">
<xsl:sort select="name()"/>
</xsl:apply-templates>
<xsl:apply-templates select="node()">
<xsl:sort select="name()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
But since the relative output order of attributes after serialization of the result tree depends on the processor, you might as well omit the attribute sorting:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()">
<xsl:sort select="name()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
Thanks Snoopy and others for their help!
i recommend xmlunit for comparing the files, this nunit extension is written in c# and free.
http://xmlunit.sourceforge.net/
or do it like this if you prefer manual compares:
XmlDocument doc = new XmlDocument(); doc.LoadXml("bla"); XmlTextWriter writer = new XmlTextWriter("data.xml",null); writer.Formatting = Formatting.Indented; doc.Save(writer);