473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2066
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
2
by: yinjennytam | last post by:
Hi all I'm learning XML and .NET and I saw an example of using XmlSerializer to deserialize an XML file to get the corresponding object in memory. I found it very useful for my purpose. ...
1
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml...
0
by: blabla120 | last post by:
Hi NG, I want to read a xml-file from an asp-page: // Kategorien aus xml laden string pathKategorien = Server.MapPath("./conf/kategorien.xml"); FileStream fs = new FileStream(pathKategorien,...
4
by: sherifffruitfly | last post by:
Hi, I have an xml file with structured like this: <?xml version="1.0" encoding="UTF-8"?> <Soldiers> <Soldier name="Billy Smith" rank="Private" serial="34" /> (a bunch more soldiers)
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
1
by: kimtherkelsen | last post by:
Hi, I want to send XML data from a server to some clients over a network connection using the TCP/IP protocol. If I send the XMLs as byte arrays I need to insert header information in the data to...
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.