Deserialize case sensitive

S

Steph

Hello,
I have a problem for Deserialize some xml file.
Some files do not necessarily respect case of the elementname.

how Deserialize with case insensitive ?

my code :

[Serializable]
[XmlRoot(ElementName = "DATA")]
public class DossierQuery
{
private string _code = "";
[XmlElement(ElementName = "code")]
public string Code
{
get { return _code; }
set { _code = value.Trim(); }
}
}

a sample file :

<data>
<CODE>qwerty</CODE>
</data>

thanks
 
M

Marc Gravell

I'm not sure that you really can (or even if it would be desirable);
perhaps fix the data instead? It simply doesn't meet the expected
contract...

Marc
 
S

Steph

Marc said:
I'm not sure that you really can (or even if it would be desirable);
perhaps fix the data instead? It simply doesn't meet the expected
contract...

Marc
sure.
i must load the file into a xmldocument, and parse each node and rebuild
a xmldocument with the good case ...
if no solution... i do it.

i will wait for a solution (few days...)

thanks.
 
J

Jon Skeet [C# MVP]

sure.
i must load the file into a xmldocument, and parse each node and rebuild
a xmldocument with the good case ...
if no solution... i do it.

i will wait for a solution (few days...)

To be honest, that *is* the solution. I would certainly *hope* that
deserialization expects valid data and will complain if you give it
garbage.

If the problems are predictable, an XSLT transform may help you.

Jon
 
M

Marc Gravell

Actually xslt might be able to do this quite easily...

<xsl:template match="data|DATA">
<DATA><xsl:apply-templates select="*"/></DATA>
</xsl:template>
<xsl:template match="code|CODE">
<code><xsl:value-of select="."/></code>
</xsl:template>

etc; depends on the real complexity, of course...

Marc
 
M

Marc Gravell

darnit, just after I post... scroll down and you beat me to it ;-p

Oh well - at least we concur...

Marc
 
J

Jon Skeet [C# MVP]

darnit, just after I post... scroll down and you beat me to it ;-p

Oh well - at least we concur...

I have to say it goes against the grain for me to suggest XSLT. In
general I find XSLT hard to both read *and* write. I dare say it's
very neat in its own way, but that doesn't actually make it pleasant
to use :)

Jon
 
M

Marc Gravell

I'd agree to a point... but used for appropriate scenarios xslt can be
a very powerful and elegant tool. Used inappropriately it is
nightmarish.

Certainly the LINQ-to-XML (for reading) and XElement etc (for writing)
might make it a lot more appealing to do the transform in code (in
Orcas, at least) moving forward; but quite possibly the xslt would be
more readable that the XmlDocument (or XmlReader/XmlWriter)
alternative.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top