2017-05-15 58 views
0

我试图将XML文件读取到C#对象中。 XML包含多个子元素。使用下面的代码我可以访问所有的顶级域(版本,现场,的pageid,等等),没有任何问题,但是当我试图访问一个子节点的值,我收到C#将XML解析为包含子元素的对象

Object reference not set to an instance of an object. 

我以为是表明XMLSerializer无法将节点与我的对象进行匹配。我已经尝试过不同的对象类型,例如数组中的List字段,但似乎仍然得到相同的结果,所以我不确定要实现此目的的最佳方法是什么?

任何帮助指出我在正确的方向将不胜感激。

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> 

对象

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

代码

... 
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); 
+0

使用LINQ到XML – Jack

+0

我看不出什么毛病的代码。我将所有内容粘贴到LinqPad并运行,它给我1和 模板1,没有例外。 –

+0

我在这里做了一个dotnetfiddle:https://dotnetfiddle.net/kDv4nF,它也可以。 –

回答

0

使用LINQ到XML解析您的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 
      }; 
+1

我试图实现这一点,但得到语法错误,说明: **找不到查询模式的实现对于源类型'XDocument'。 'Select'not found。** 我添加了对System.Linq和System.Xml.Linq的引用。 – logicallysynced

+0

好吧我用** doc.Descendants(“LiveModelStruct”)解决了上述问题** – logicallysynced

+0

@logicallysynced如果您的问题解决了,如果可以的话标记为已解决。 – Jack