2

I am attempting to read an XML file into a C# object. The XML contains multiple child elements. Using the below code I can access all the top fields (version, live, pageid, etc.) with no problems, however when I try to access a child node value, I receive a

Object reference not set to an instance of an object.

which I assume is indicating that XMLSerializer can't match the nodes with my object. I have tried different object types such as a List field over an array but still seem to get the same results, so I'm unsure what the best way to approach this is?

Any help pointing my in the right direction would be much appreciated.

XML

<?xml version="1.0" encoding="utf-8"?>
  <LiveModelStruct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <version>1</version>
   <live>true</live>
   <pageid>1</pageid>
   <data>test data</data>
   <giveawayactive>false</giveawayactive>
   <giveawayserial>00000000</giveawayserial>
   <templates>
     <template>
       <id>1</id>
       <title>Template 1</title>
       <type>Opener</type>
     </template>
     <template>
       <id>2</id>
       <title>Template 2</title>
       <type>Song</type>
     </template>
  </templates>
 </LiveModelStruct>

Object

[XmlRoot(ElementName = "LiveModelStruct")]
public class LiveModelStruct
{
    [XmlElement(ElementName = "version")]
    public string version { get; set; }
    [XmlElement(ElementName = "live")]
    public string live { get; set; }
    [XmlElement(ElementName = "pageid")]
    public string pageid { get; set; }
    [XmlElement(ElementName = "data")]
    public string data { get; set; }
    [XmlElement(ElementName = "giveawayactive")]
    public string giveawayactive { get; set; }
    [XmlElement(ElementName = "giveawayserial")]
    public string giveawayserial { get; set; }
    [XmlElement(ElementName = "templates")]
    public Templates Templates { get; set; }
    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }
    [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsd { get; set; } 
}

[XmlRoot(ElementName = "templates")]
public class Templates
{
    [XmlElement(ElementName = "template")]
    public Template[] Template { get; set; }    
}

[XmlRoot(ElementName = "template")]
public class Template
{
    [XmlElement(ElementName = "id")]
    public string id { get; set; }
    [XmlElement(ElementName = "title")]
    public string title { get; set; }
    [XmlElement(ElementName = "type")]
    public string type { get; set; }
}

Code

...
var serializer = new XmlSerializer(typeof(LiveModelStruct));
var data = (LiveModelStruct)serializer.Deserialize(stream);
var test1 = data.version;
Debug.WriteLine(test1); //Returns 1 as it should

var test = data.Templates.Template[0].title; //Throws Error
Debug.WriteLine(test);

1 Answers1

2

Use LINQ to XML to parse your XML.

Parse xml using LINQ to XML to class objects

XDocument doc = XDocument.Parse(xml);
var result = from c in doc.Descendants("LiveModelStruct")
             select new LiveModelStruct()
             {
                    version = (string)c.Element("version").Value,
                    live = (string)c.Element("live").Value
             };
Community
  • 1
  • 1
Jack
  • 5,680
  • 10
  • 49
  • 74
  • 1
    I'm attempting to implement this, but getting a syntax error stating: **Could not find an implementation of the query pattern for source type 'XDocument'. 'Select' not found.** I have added references to System.Linq and System.Xml.Linq. – logicallysynced May 15 '17 at 09:48
  • Okay I fixed the above issue with **doc.Descendants("LiveModelStruct")** – logicallysynced May 15 '17 at 10:11
  • @logicallysynced If your issue is solved, mark as solved if you can. – Jack May 15 '17 at 15:39
  • In the end I used JSON over XML to resolve this, as I think Xamarin was causing issues with both LINQ and my own code (which apparently worked fine in a normal .NET application). – logicallysynced May 15 '17 at 16:01
  • @logicallysynced Good choice. Unless you need something XML can do that JSON can't, XML is more complicated to use. Use JSON.NET to serialize and deserialize objects easily. – Jack May 15 '17 at 17:18