Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Deserialize case sensitive

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steph - 10 Jan 2008 07:32 GMT
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
Marc Gravell - 10 Jan 2008 08:01 GMT
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
Steph - 10 Jan 2008 08:52 GMT
> 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.
Jon Skeet [C# MVP] - 10 Jan 2008 09:09 GMT
> 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
Marc Gravell - 10 Jan 2008 10:23 GMT
darnit, just after I post... scroll down and you beat me to it ;-p

Oh well - at least we concur...

Marc
Jon Skeet [C# MVP] - 10 Jan 2008 10:35 GMT
> 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
Marc Gravell - 10 Jan 2008 10:50 GMT
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
Marc Gravell - 10 Jan 2008 10:22 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.