Xml deserializitaion trouble ... Please advise

  • Thread starter Nikolay Anestev
  • Start date
N

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
 
C

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!
*************************************************
 

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