【发布时间】:2017-10-14 00:19:21
【问题描述】:
我正在尝试将 XML 文件读入 C# 对象。 XML 包含多个子元素。使用下面的代码,我可以毫无问题地访问所有顶部字段(版本、实时、pageid 等),但是当我尝试访问子节点值时,我收到一个
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);
【问题讨论】:
-
使用 LINQ to XML
-
我看不出代码有什么问题。我将所有内容粘贴到 LinqPad 中并运行它,它给了我 1 和模板 1,没有例外。
-
我在这里做了一个dotnetfiddle:dotnetfiddle.net/kDv4nF,它也可以工作。
-
@PalleDue - 我想知道使用 Xamarin 是否会导致它无法正常工作.. :/