简体   繁体   English

Xamarin:反序列化XML-“ XML文档中存在错误”

[英]Xamarin: Deserializing XML - “There is an error in XML document”

In order to use some XML files in my Xamarin-Forms project, I'm trying to recreate the steps given in this example code , however I always get the error message: 为了在Xamarin-Forms项目中使用一些XML文件,我试图重新创建此示例代码中给出的步骤,但是我总是收到错误消息:

An exception of type 'System.InvalidOperationException' occurred in System.Xml.XmlSerializer.dll but was not handled in user code System.Xml.XmlSerializer.dll中发生类型为'System.InvalidOperationException'的异常,但未在用户代码中处理

Additional information: There is an error in XML document (2, 2). 附加信息:XML文档(2、2)中有错误。

The example code works fine by the way. 该示例代码可以正常工作。

This is the XML file I use (as an embedded resource): 这是我使用的XML文件(作为嵌入式资源):

<?xml version="1.0" encoding="utf-8" ?>
<Items>
  <Item>
    <Name>One</Name>
    <State>Alpha</State>
  </Item>
  <Item>
    <Name>Two</Name>
    <State>Two</State>
  </Item>
</Items>

This is the C# code I use: 这是我使用的C#代码:

using System;
using Xamarin.Forms;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace XmlTestProject
{
    public class XmlContentPage : ContentPage
    {
        public XmlContentPage()
        {

            //get access to xml file
            var assembly = GetType().GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("XmlTestProject.XmlFile.xml");
            List<Item> items;
            using (var reader = new System.IO.StreamReader(stream))
            {
                var serializer = new XmlSerializer(typeof(List<Item>));
                items = (List<Item>)serializer.Deserialize(reader);
            }
            var listView = new ListView();
            listView.ItemsSource = items;

            Content = new StackLayout
            {
                Children = {
                    listView
                }
            };
        }
    }

    public class Item
    {
        public string Name { get; set; }
        public string State { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}

I'm using Visual Studio 2015 community edition and Xamarin.Forms 2.2.0.5-pre2 我正在使用Visual Studio 2015社区版和Xamarin.Forms 2.2.0.5-pre2

The xml should have ArrayOfItem instead of Items : xml应该具有ArrayOfItem而不是Items

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfItem>
  <Item>
    <Name>One</Name>
    <State>Alpha</State>
  </Item>
  <Item>
    <Name>Two</Name>
    <State>Two</State>
  </Item>
</ArrayOfItem>

try declaring a wrapper class for your list, and then deserialize like this: 尝试为您的列表声明一个包装器类,然后像这样反序列化:

var serializer = new XmlSerializer(typeof(ItemList));
ItemList items = (ItemList)serializer.Deserialize(reader);

listView.ItemsSource = items.Items;

[XmlRoot("Items")]
public class ItemList
{
    public ItemList() {Items = new List<Item>();}
    [XmlElement("Item")]
    public List<Item> Items {get;set;}
}

public class Item
{
    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("State")]
    public string State { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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