473,220 Members | 2,151 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,220 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 2056
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.