Search code examples
c#xml-deserialization

Deserialize XML values to objects


I'm trying to use XDocument to read in XML and deserialize it to objects.

I want to take the following XML:

<r25:spaces xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xl="http://www.w3.org/1999/xlink" xmlns:r25="http://www.collegenet.com/r25" pubdate="2019-07-15T14:51:16-07:00" engine="accl">
  <r25:space crc="00000022" status="est" xl:href="space.xml?space_id=200">
    <r25:space_id>200</r25:space_id>
    <r25:space_name>LRN 0001</r25:space_name>
    <r25:formal_name>Learning Commons -Test Scheduling Room</r25:formal_name>
    <r25:partition_id xl:href="rmpart.xml?partition_id=2">2</r25:partition_id>
    <r25:last_mod_dt>2019-07-11T08:01:00-07:00</r25:last_mod_dt>
  </r25:space>
</r25:spaces>

and deserialize it to a List of spaces (where Space has the following definition):

public class Space
{
    public long space_id { get; set; }
    public string space_name { get; set; }
    public string formal_name { get; set; }
    public long partition_id { get ; set; }
    public DateTime last_mod_dt { get; set; }
}

I've only gotten so far as to get the XElement. It dies on the serializer

var doc = XDocument.Parse(result.Content);
XNamespace ns = "http://www.collegenet.com/r25";
XElement el = doc.Element(ns + "spaces");

foreach (var space in el.Elements())
{
    var serializer = new XmlSerializer(typeof(Space));
    var s = (Space)serializer.Deserialize(space.CreateReader());
}

Solution

  • I found a website that will take XML and create the proper deserialization classes. Xml2CSharp, which created the following classes that allowed the deserialization to work:

    [XmlRoot(ElementName="partition_id", Namespace="http://www.collegenet.com/r25")]
    public class Partition_id {
        [XmlAttribute(AttributeName="href", Namespace="http://www.w3.org/1999/xlink")]
        public string Href { get; set; }
        [XmlText]
        public string Text { get; set; }
    }
    
    [XmlRoot(ElementName="space", Namespace="http://www.collegenet.com/r25")]
    public class Space {
        [XmlElement(ElementName="space_id", Namespace="http://www.collegenet.com/r25")]
        public string Space_id { get; set; }
        [XmlElement(ElementName="space_name", Namespace="http://www.collegenet.com/r25")]
        public string Space_name { get; set; }
        [XmlElement(ElementName="formal_name", Namespace="http://www.collegenet.com/r25")]
        public string Formal_name { get; set; }
        [XmlElement(ElementName="partition_id", Namespace="http://www.collegenet.com/r25")]
        public Partition_id Partition_id { get; set; }
        [XmlElement(ElementName="last_mod_dt", Namespace="http://www.collegenet.com/r25")]
        public string Last_mod_dt { get; set; }
        [XmlAttribute(AttributeName="crc")]
        public string Crc { get; set; }
        [XmlAttribute(AttributeName="status")]
        public string Status { get; set; }
        [XmlAttribute(AttributeName="href", Namespace="http://www.w3.org/1999/xlink")]
        public string Href { get; set; }
    }
    
    [XmlRoot(ElementName="spaces", Namespace="http://www.collegenet.com/r25")]
    public class Spaces {
        [XmlElement(ElementName="space", Namespace="http://www.collegenet.com/r25")]
        public Space Space { get; set; }
        [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName="xl", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xl { get; set; }
        [XmlAttribute(AttributeName="r25", Namespace="http://www.w3.org/2000/xmlns/")]
        public string R25 { get; set; }
        [XmlAttribute(AttributeName="pubdate")]
        public string Pubdate { get; set; }
        [XmlAttribute(AttributeName="engine")]
        public string Engine { get; set; }
    }