【发布时间】: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