XML文档C#Xamarin中存在错误

时间:2017-07-09 13:58:03

标签: c# xamarin xamarin.forms

我在Xamarin Forms应用程序中收到以下错误,但我不知道问题所在:

System.InvalidOperationException:XML文档中存在错误。  items xmlns =''>没想到

我有一个基本的XML文件,我已将其添加为嵌入式资源:

<?xml version="1.0" encoding="UTF-8"?>
  <items>
    <item>
        <id>1</id>
        <name>a name</name>
        <address>an address</address>
        <postcode>a postcode</postcode>
    </item>
    <item>
        <id>2</id>
        <name>name 2</name>
        <address>address 2</address>
        <postcode>postcode 2</postcode>
    </item>
    <item>
        <id>3</id>
        <name>name 3</name>
        <address>address 3</address>
        <postcode>postcode 3</postcode>
    </item>
</items>

我有以下方法来读取XML文件:

    public static List<Item> GetItemList()
    {
        var assembly = typeof(MyNewPage).GetTypeInfo().Assembly;
        Stream stream = assembly.GetManifestResourceStream("MyNewApp.allitems.xml");

        List<Item> itemsFullList;

        using (var reader = new System.IO.StreamReader(stream))
        {
            var serializer = new XmlSerializer(typeof(List<Item>));
            itemsFullList = (List<Item>)serializer.Deserialize(reader);
        }

        return itemsFullList;
    }

我还有一个标准类来代表每个项目:

public class Item
{
    public Item()
    {
    }

    public string name { get; set; }
    public string address { get; set; }
    public string postcode { get; set; }

}

我不知道为什么我从我能看到的错误中得到错误,XML文档格式化得很好。我使用这篇文章作为指南,但我没有运气:{{3}}

任何人都知道我在这里做错了吗?

感谢。

1 个答案:

答案 0 :(得分:1)

该类结构不会反序列化为Item列表,它将反序列化为具有类型Item[]属性的单个对象。

您可能希望更改类和属性名称以使其更有意义,但这是Visual Studio基于您的xml生成类结构的方式(编辑&gt;选择性粘贴)

void Main()
{
    string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  <items>
    <item>
        <id>1</id>
        <name>a name</name>
        <address>an address</address>
        <postcode>a postcode</postcode>
    </item>
    <item>
        <id>2</id>
        <name>name 2</name>
        <address>address 2</address>
        <postcode>postcode 2</postcode>
    </item>
    <item>
        <id>3</id>
        <name>name 3</name>
        <address>address 3</address>
        <postcode>postcode 3</postcode>
    </item>
</items>";

    using (MemoryStream ms = new MemoryStream())
    {
        XDocument.Parse(xml).Save(ms);
        ms.Position = 0;
        using (var reader = new System.IO.StreamReader(ms))
        {
            var serializer = new XmlSerializer(typeof(items));
            var itemsFullList = (items)serializer.Deserialize(reader);
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(ElementName = "items", Namespace = "", IsNullable = false)]
public partial class items
{

    private itemsItem[] itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("item")]
    public itemsItem[] item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItem
{

    private byte idField;

    private string nameField;

    private string addressField;

    private string postcodeField;

    /// <remarks/>
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    public string address
    {
        get
        {
            return this.addressField;
        }
        set
        {
            this.addressField = value;
        }
    }

    /// <remarks/>
    public string postcode
    {
        get
        {
            return this.postcodeField;
        }
        set
        {
            this.postcodeField = value;
        }
    }
}