472,133 Members | 1,162 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

reading handwritten XML using XmlSerializer

I'm porting an application from Apache Xerces to .Net and am having a couple
of small problems with deserialization. The XML that I'm reading comes from
a variety of sources, and there are two inconsistencies that Xerces is able
to handle that XmlSerializer seems not to be able to deal with.

1) Some of the boolean values that I'm dealing with are "True" and "False"
rather than "true" and "false". I'm not quite sure whether "True" and
"False" are non-standard, but they don't process in XmlSerializer.

2) The XML I'm reading uses attributes and entities interchangably - that
is, I might see either <foo bar="baz"/> or <foo><bar>baz</bar></foo> and they
mean the same thing. With XmlSerializer, I can either do one or the other,
e.g. class foo { [XmlAttribute()]public string bar; } or I can leave the
XmlAttribute out. But I can't get it to accept both possibilities at the
same time.

I'm tossing around 2 options now - one is to effectively write my own
deserialization mechanism and the other is to cook up some XSLT to translate
the incoming XML into something that XmlSerializer can handle. Neither of
those options are very pleasing.

Is there a third possibility?

Jun 2 '06 #1
1 1966
SteveB wrote:
I'm porting an application from Apache Xerces to .Net and am having a couple
of small problems with deserialization. The XML that I'm reading comes from
a variety of sources, and there are two inconsistencies that Xerces is able
to handle that XmlSerializer seems not to be able to deal with.

1) Some of the boolean values that I'm dealing with are "True" and "False"
rather than "true" and "false". I'm not quite sure whether "True" and
"False" are non-standard, but they don't process in XmlSerializer.
If the Schema or DTD specifies that "true" and "false" are the valid
values, then a document using "True" and "False" is invalid and should
not be processed any further, but rejected and sent back to the source
for correction.

If there is no Schema or DTD then the best you can do is either

a) preconvert all to lowercase before processing
or
b) perform all tests using a caseless comparison
2) The XML I'm reading uses attributes and entities interchangably - that
is, I might see either <foo bar="baz"/> or <foo><bar>baz</bar></foo>
There are no entities in that example. Did you mean elements?
and they mean the same thing.
Then the document has been composed by someone who doesn't have any clue
what they are doing. The two examples above are very much *not* the
same thing. I do hope your customer is not doing anything
business-critical with this data, because they may be jeopardising their
company by playing fast and loose with its data.
With XmlSerializer, I can either do one or the other,
e.g. class foo { [XmlAttribute()]public string bar; } or I can leave the
XmlAttribute out. But I can't get it to accept both possibilities at the
same time.
That's because they are not equivalent expressions. You need to decide
whether you want to use an attribute or a subelement (see the FAQ on
this at http://xml.silmaril.ie/developers/attributes/) and preconvert
the document accordingly.
I'm tossing around 2 options now - one is to effectively write my own
deserialization mechanism and the other is to cook up some XSLT to translate
the incoming XML into something that XmlSerializer can handle. Neither of
those options are very pleasing.


The XSLT is quite straightforward if those are the only two things to
fix. If your XML document looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<doc>
<stuff>
<whatever id="abc" something="true">
<foo bar="baz"/>
</whatever>
<whatever id="xyz" something="False">
<foo><bar>blort</bar></foo>
</whatever>
</stuff>
</doc>

then your XSLT document looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="whatever">
<xsl:copy>
<!-- output all attributes EXCEPT @something -->
<xsl:for-each select="@*[not(name()='something')]">
<xsl:copy/>
</xsl:for-each>
<!-- now do @something, converting to lowercase -->
<xsl:attribute name="something">
<xsl:value-of
select="translate(@something,'AEFLRSTU','aeflrstu' )"/>
</xsl:attribute>
<!-- process any children -->
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="foo">
<xsl:copy>
<xsl:choose>
<!-- if there is a child element named bar,
output it as an attribute -->
<xsl:when test="bar">
<xsl:attribute name="bar">
<xsl:value-of select="bar"/>
</xsl:attribute>
</xsl:when>
<!-- otherwise just output what's there -->
<xsl:otherwise>
<xsl:copy-of select="@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>

<!-- identity transform to handle everything else -->

<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Jun 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JohnnySparkles | last post: by
2 posts views Thread by yinjennytam | last post: by
4 posts views Thread by sherifffruitfly | last post: by
2 posts views Thread by =?Utf-8?B?U2hhd24=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.