【问题标题】:Xamarin: Deserializing XML - "There is an error in XML document"Xamarin:反序列化 XML - “XML 文档中存在错误”
【发布时间】:2016-04-19 21:22:36
【问题描述】:

为了在我的 Xamarin-Forms 项目中使用一些 XML 文件,我正在尝试重新创建此 example code 中给出的步骤,但是我总是收到错误消息:

System.Xml.XmlSerializer.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

附加信息:XML 文档中存在错误 (2, 2)。

顺便说一下,示例代码运行良好。

这是我使用的 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>

这是我使用的 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;
        }
    }
}

我正在使用 Visual Studio 2015 社区版和 Xamarin.Forms 2.2.0.5-pre2

【问题讨论】:

    标签: c# xml xamarin xamarin.forms


    【解决方案1】:

    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>
    

    【讨论】:

      【解决方案2】:

      尝试为您的列表声明一个包装类,然后像这样反序列化:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-28
        • 1970-01-01
        • 2014-07-11
        • 2011-06-11
        • 2021-11-05
        • 1970-01-01
        相关资源
        最近更新 更多