Deserialize case sensitive

  • Thread starter Thread starter Steph
  • Start date Start date
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
 
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
 
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.
 
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
 
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
 
darnit, just after I post... scroll down and you beat me to it ;-p

Oh well - at least we concur...

Marc
 
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
 
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
 
Back
Top