Parsing XML into .Net class
I have received a specification for a webservice call which looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<methodResponse>
<params>
<param>
<value>
<array>
<data>
<value>
<struct>
<member>
<name>OrderNumber</name>
<value>
<string>101</string>
</value>
</member>
<member>
<name>Created</name>
<value>
<string>2010-11-01 11:00:00</string>
</value>
</member>
<member>
<name>Rows</name>
<value>
<array>
<data>
<value>
<struct>
<member>
<name>ProductNumber</name>
<value>
<string>prod1</string>
</value>
</member>
<member>
<name>Title</name>
<value>
<string>Produkt 1</string>
</value>
开发者_JAVA百科</member>
</struct>
</value>
Is this some kind of standard serialize format? (I think the service is written in php)
Any good ideas how to extract the information into a .net class?
Edit:
I solved it with an xslt transform which creates a more "normal" xml file
<Orders>
<struct>
<OrderNumber>101</OrderNumber>
<Created>2010-11-01 11:00:00</Created>
<Rows>
<struct>
<ProductNumber>prod1</ProductNumber>
...
From the result it was easy to create a schema and generate a .net class with xsd.exe.
The transform looks like:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="struct"/>
<xsl:template match="/">
<Orders>
<xsl:apply-templates/>
</Orders>
</xsl:template>
<xsl:template match="struct">
<struct>
<xsl:apply-templates/>
</struct>
</xsl:template>
<xsl:template match="value/string|value/double">
<xsl:value-of select="self::*"/>
</xsl:template>
<xsl:template match="//member">
<xsl:element name="{name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Is this some kind of standard serialize format?
I really hope not, it is horrible. Deserving of publicity at http://thedailywtf.com/
Any good ideas how to extract the information into a .net class?
Assuming the sample is incomplete and the real thing is valid XML, any of the .NET XML APIs will be able to do it. (The XML serialisers, including Data Contract, are unlikely to work without a lot of effort to create wrapper types for all the extra levels of elements being seen there.)
Perhaps pointing the writers of this "service" at OData to show what is possible rather than reinventing it extremely badly.
I found out that it is a standard format called XML-RPC which got its own tag here on SO. Official site Wikipedia
There is a .Net library available
精彩评论