0

I have following xml

<result>
    <key accessMask="4294967295" type="Account" expires="">
        <rowset name="characters" key="characterID" columns="characterID,characterName,corporationID,corporationName,allianceID,allianceName,factionID,factionName">
            <row characterID="123" characterName="Sefa 123" corporationID="456" corporationName="Signal Cartel" allianceID="159" allianceName="Scouts" factionID="0" factionName=""/>
            <row characterID="1234" characterName="Sefa 1234" corporationID="987" corporationName="Havos" allianceID="753" allianceName="Unlimited" factionID="0" factionName=""/>
        </rowset>
     </key>
</result>

And i have following Schema setup for deserializing this output.

    [XmlRoot("result")]
    public class ApiKeyInfo
    {
        [XmlElement("key")]
        public Key Key { get; set; }
    }

    public class Key
    {
        [XmlAttribute("accessMask")]
        public long AccessMask { get; set; }

        [XmlAttribute("type")]
        public string Type { get; set; }

        [XmlElement("rowset")]
        public List<AccountCharacter> Characters { get; set; }
    }

    public class AccountCharacter
    {
        [XmlAttribute("characterId")]
        public long Id { get; set; }

        [XmlAttribute("characterName")]
        public string Name { get; set; }

        [XmlAttribute("corporationID")]
        public long CorpId { get; set; }

        [XmlAttribute("corporationName")]
        public string CorpName { get; set; }

        [XmlAttribute("allianceID")]
        public long AllianceId { get; set; }

        [XmlAttribute("allianceName")]
        public string AllianceName { get; set; }

        [XmlAttribute("factionID")]
        public long FactionId { get; set; }

        [XmlAttribute("factionName")]
        public string FactionName { get; set; }
    }

Problem is, i can't deserialize character informations. Characters list is always containing 0 elements inside.

1 Answer 1

3

This:

[XmlElement("rowset")]
public List<AccountCharacter> Characters { get; set; }

Implies that you'll have a bunch of rowset elements, e.g:

<rowset characterID="123" characterName="Sefa 123" ...

Try this instead:

[XmlArray("rowset")]
[XmlArrayItem("row")]
public List<AccountCharacter> Characters { get; set; }

A useful tip for these sort of deserialisation problems is to try the process in reverse: create an object and serialise it and see what the XML looks like. It will likely give you some indication as to where your problem is.

See this fiddle for a working demo, it prints both row name values to the console.

Sign up to request clarification or add additional context in comments.

3 Comments

I mark this as solution because xml output is same as my input when i decorated my class as you show. But i still get 0 objects in my Characters list. I'm still not able to deserialize them into my list it seems.
@deusExCore you can see in this fiddle that it works fine. Not sure what you're doing differently!
Looks pretty much the same :) I'm using Restsharp to send request and deserialize the response for me. Maybe it's a problem with the Restsharp. Thanks for the answer and fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.