简体   繁体   中英

C# Parse XML into Object with child elements

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);

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
             };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM