在C#中反序列化Xml的问题

时间:2019-07-11 17:10:55

标签: c# android xml xamarin

我正在按照here给出的教程进行反序列化嵌入的xml文档。

我的Xml文档:

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfAgency xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DbAgencyDefinition>
    <Name>RTD</Name>
    <Country>USA</Country>
    <City>Denver</City>
    <State>CO</State>
    <GtfsZipUrlDirectory>http://www.address.com/etc/</GtfsZipUrlDirectory>
    <GtfsZipUrlFileName>file_name.zip</GtfsZipUrlFileName>
  </DbAgencyDefinition>
</ArrayOfAgency>

我要反序列化的班级:

public class DbAgencyDefinition
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string GtfsZipUrlDirectory { get; set; }
        public string GtfsZipUrlFileName { get; set; }
        public string State { get; set; }
    }

试图将XML反序列化为DbAgencyDefinition列表的代码:

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(DbAgencyDefinition)).Assembly;
            Stream stream = assembly.GetManifestResourceStream("MyNamespace.Resources.xml.AgencyDefinitions.xml");
            var agencies = new List<DbAgencyDefinition>();
            using (var reader = new StreamReader(stream))
            {
                var serializer = new XmlSerializer(typeof(List<DbAgencyDefinition>));
                agencies = (List<DbAgencyDefinition>)serializer.Deserialize(reader);
            }

我得到的错误是:

System.Exception: There is an error in XML document. <ArrayOfAgency xmlns=''> was not expected

我用XML尝试了上百万次,将类标记为Serializable,它总是返回此错误。我查看了教程提供的代码示例,但无法弄清楚为什么会出现此错误。

2 个答案:

答案 0 :(得分:1)

Windows以及Windows上的

VS都有一个特殊的工具,可以将复制的Xml转换为自动生成的类。现在,它并不完美,但是如果您使用Xml文件,它会生成类似于以下内容的两个类:

public class ArrayOfAgency
{
    public ArrayOfAgencyDbAgencyDefinition DbAgencyDefinition { get; set; }
}

public class ArrayOfAgencyDbAgencyDefinition
{
    public string Name { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string GtfsZipUrlDirectory { get; set; }
    public string GtfsZipUrlFileName { get; set; }
}

您可能会注意到,ArrayOfAgency被确定为持有DbAgencyDefinition的类,这就是为什么它在尝试将其直接反序列化为List<DbAgencyDefinition>时抛出错误的原因。类型和序列化程序的期望并不完全相同。

var serializer = new XmlSerializer(typeof(ArrayOfAgency));
var agencies = ((ArrayOfAgency)serializer.Deserialize(reader)).DbAgencyDefinition;

另外,正如我提到的那样,自动生成可能并不完美,因为如果Xml中可能有多个ArrayOfAgencyDbAgencyDefinition可能需要保存一个数组而不是直接类。 / p>

public class ArrayOfAgency
{
    public ArrayOfAgencyDbAgencyDefinition[] DbAgencyDefinition { get; set; }
}

如果您需要有关Xml序列化的更多帮助或信息,请查看docs

答案 1 :(得分:0)

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication120
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            using (var reader = new StreamReader(FILENAME, Encoding.UTF8))
            {
                var serializer = new XmlSerializer(typeof(ArrayOfAgency));
                ArrayOfAgency agencies = (ArrayOfAgency)serializer.Deserialize(reader);
            }

        }
    }
    public class ArrayOfAgency
    {
        public DbAgencyDefinition DbAgencyDefinition { get; set; }
    }
    public class DbAgencyDefinition
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string GtfsZipUrlDirectory { get; set; }
        public string GtfsZipUrlFileName { get; set; }
        public string State { get; set; }
    }

}