3

I am trying to deserialize the XML document:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2013-07-07 07:24:20</currentTime>
  <result>
    <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
      <row name="xxxxx" characterID="1234" corporationName="xxxx" corporationID="1234" />
    </rowset>
  </result>
  <cachedUntil>2013-07-07 07:40:39</cachedUntil>
</eveapi>

My model is:

[XmlRoot("rowset")]
public class CharacterList
{
    public CharacterList() { Characters = new List<Character>(); }

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

public class Character
{
    [XmlElement("name")]
    private string name { get; set; }

    [XmlElement("characterID")]
    private int Id { get; set; }

    [XmlElement("corporationName")]
    private string corporationName { get; set; }

    [XmlElement("corporationID")]
    private int corporationId { get; set; }
}

My deserialization code is:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "result";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(Character), xRoot);
var list = (CharacterList) serializer.Deserialize(output);

However, I am getting an exception:

System.InvalidOperationException: There is an error in XML document (2,2).

with an inner type:

System.InvalidOperationException: <eveapi xmlns=''> was not expected.

I'm pretty sure this is because of the outer information I do not need. Is there a way I can ignore it? my other thought was I could write wrapper classes for the rest of the schema, then just ignore what I don't care about. However, I was hoping there is an easier way. I've been stuck on this for a while, any help would be appreciated.

Paul Spinelli
  • 33
  • 1
  • 3

2 Answers2

2

You can use an XmlReader to navigate to the inner element and use the XmlSerializer from there:

   using (XmlReader reader = XmlReader.Create("c:\\your.xml"))
    {
        reader.MoveToContent();
        reader.ReadToDescendant("rowset");
        var serializer = new XmlSerializer(typeof(CharacterList));
        var list = (CharacterList)serializer.Deserialize(reader);
    }

Please also note that there are also some issues in your model:

  • Properties should be public.
  • Use XmlAttribute for attributes instead of XmlElement
  • Use typeof(CharacterList) instead of typeof(Character)

    [XmlRoot("rowset")]
    public class CharacterList
    {
        public CharacterList() { Characters = new List<Character>(); }
    
        [XmlElement("row")]
        public List<Character> Characters { get; set; }
    }
    
    public class Character
    {
        [XmlAttribute("name")]
        public  string name { get; set; }
    
        [XmlAttribute("characterID")]
        public int Id { get; set; }
    
        [XmlAttribute("corporationName")]
        public string corporationName { get; set; }
    
        [XmlAttribute("corporationID")]
        public int corporationId { get; set; }
    }
    
fcuesta
  • 4,429
  • 1
  • 18
  • 13
1

I'm not used to XMLSerialzer, but what about using Linq-to-XML instead?

        string pathToXML = "";
        XDocument doc = XDocument.Load(pathToXML);

        var qry = from ele in doc.Descendants("row")
                     select new
                     {
                         name = ele.Attribute("name").Value,
                         charID = Convert.ToInt32(ele.Attribute("characterID").Value),
                         corName = ele.Attribute("corporationName").Value,
                         corID =  Convert.ToInt32(ele.Attribute("corporationID").Value)
                     };

        foreach (var element in qry)
        {
            Console.WriteLine(element.name + " " + element.charID + " " + element.corName + " " + element.corID);
        }
Philip W
  • 781
  • 3
  • 7