Home All Groups Group Topic Archive Search About

Xml deserializitaion trouble ... Please advise

Author
9 Jun 2006 11:22 AM
Nikolay Anestev
Hi all. I have the following xml content:

<?xml version="1.0"?>
<_ARRAY LangCode="2">
    <LABELS Count="4">
        <code>Code</code>
        <ident>Ident</ident>
    </LABELS>
    <USERS_ACCOUNTS>
        <code>14403</code>
        <ident>1714 358113156 01 1</ident>
    </USERS_ACCOUNTS>
    <USERS_ACCOUNTS>
        <code>14403</code>
        <ident>1714 358113156 09 7</ident>
    </USERS_ACCOUNTS>
</_ARRAY>

, that must be serialized into the following types:

[XmlRoot("USERS_ACCOUNTS")] public class Account
{
    private int code;
    private string ident;

    [XmlElement("code")] public int Code
    {
        get{ return code; }
        set{ code = value; }
    }
    [XmlElement("ident")] public string Ident
    {
        get{ return ident; }
        set{ ident = value; }
    }
}

[XmlRoot("_ARRAY"), XmlInclude(typeof(Account))] public class Accounts
{
    [XmlAnyElement]
    public XmlElement[] AllElements;
    [XmlArrayItem("USERS_ACCOUNTS")]
    public Account[] List;
}

, where the serialization code looks like this:
XmlSerializer serializer = new XmlSerializer( typeof( Accounts ) );
Accounts accs = (Accounts)serializer.Deserialize( new StringReader( Xml ) );

The result is that all the three nodes in the xml are serialized as unknown
types as items of the AllElements array, while I want to serialize the two
USERS_ACCOUNTS as array items of the List array.

What am I doing wrong? This drove me crazy already. Thank you in advance.

Nikolay Anestev

Author
9 Jun 2006 12:46 PM
Cowboy (Gregory A. Beamer)
I would actually have to play with the code a bit to get a better answer,
but with just a cursory glance, it would be havinv USER_ACCOUNTS at the smae
level as LABEL, making both types of tags siblings. If you were to wrap the
USERS_ACCOUNTS in a tag, it would put them at another level.

If I get a chance to experiment (doubtful with my current low bandwidth), I
will see if there is an XPath way to solve without changing your XML or
reverting to a custom deserializer.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
Show quote
"Nikolay Anestev" <1**@456.com> wrote in message
news:eM3H4b7iGHA.1508@TK2MSFTNGP04.phx.gbl...
> Hi all. I have the following xml content:
>
> <?xml version="1.0"?>
> <_ARRAY LangCode="2">
>    <LABELS Count="4">
>        <code>Code</code>
>        <ident>Ident</ident>
>    </LABELS>
>    <USERS_ACCOUNTS>
>        <code>14403</code>
>        <ident>1714 358113156 01 1</ident>
>    </USERS_ACCOUNTS>
>    <USERS_ACCOUNTS>
>        <code>14403</code>
>        <ident>1714 358113156 09 7</ident>
>    </USERS_ACCOUNTS>
> </_ARRAY>
>
> , that must be serialized into the following types:
>
> [XmlRoot("USERS_ACCOUNTS")] public class Account
> {
>    private int code;
>    private string ident;
>
>    [XmlElement("code")] public int Code
>    {
>        get{ return code; }
>        set{ code = value; }
>    }
>    [XmlElement("ident")] public string Ident
>    {
>        get{ return ident; }
>        set{ ident = value; }
>    }
> }
>
> [XmlRoot("_ARRAY"), XmlInclude(typeof(Account))] public class Accounts
> {
>    [XmlAnyElement]
>    public XmlElement[] AllElements;
>    [XmlArrayItem("USERS_ACCOUNTS")]
>    public Account[] List;
> }
>
> , where the serialization code looks like this:
> XmlSerializer serializer = new XmlSerializer( typeof( Accounts ) );
> Accounts accs = (Accounts)serializer.Deserialize( new StringReader(
> Xml ) );
>
> The result is that all the three nodes in the xml are serialized as
> unknown types as items of the AllElements array, while I want to serialize
> the two USERS_ACCOUNTS as array items of the List array.
>
> What am I doing wrong? This drove me crazy already. Thank you in advance.
>
> Nikolay Anestev
>
>

AddThis Social Bookmark Button