简体   繁体   English

将 Xml 反序列化为列表<T> - xmlns=&#39;&#39; 不是预期的

[英]Deserializing Xml to List<T> - xmlns='' was not expected

I have problems with deserializing XML to the List of locations objects.我在将 XML 反序列化为位置对象列表时遇到问题。
Given the following XML:给定以下 XML:

<?xml version="1.0" encoding="utf-8"?>
    <locations>
        <location id="1">
            <level name="3" complete="True" stars="1" firstMisson="True" secondMission="False" thridMission="False" />
        </location>
        <location id="2">
           <level name="4" complete="True" stars="3" firstMisson="True" secondMission="True" thridMission="True" />
        </location>
    </locations>

And the following class:以及以下课程:

    [System.Serializable]
    [XmlRoot(ElementName = "level")]
    public class Level
    {
        [XmlAttribute(AttributeName = "name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName = "complete")]
        public string Complete { get; set; }
        [XmlAttribute(AttributeName = "stars")]
        public string Stars { get; set; }
        [XmlAttribute(AttributeName = "firstMisson")]
        public string FirstMisson { get; set; }
        [XmlAttribute(AttributeName = "secondMission")]
        public string SecondMission { get; set; }
        [XmlAttribute(AttributeName = "thridMission")]
        public string ThridMission { get; set; }
    }
    [System.Serializable]
    [XmlRoot(ElementName = "location")]
    public class Location
    {
        [XmlElement(ElementName = "level")]
        public Level Level { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }
    [System.Serializable]
    [XmlRoot(ElementName = "locations")]
    public class Locations
    {
        [XmlElement(ElementName = "location")]
        public Location Location { get; set; }

        public List<Locations> LocDb = new List<Locations>();
    }
    [System.Serializable]
    [XmlRoot(ElementName = "xml")]
    public class Xml
    {
        [XmlElement(ElementName = "locations")]
        public Locations Locations { get; set; }
    }

Deserialization Method反序列化方法

     public List<Locations> locDB = new List<Locations>();

     public static void LoadData()
        {
            string filepath = Application.dataPath + @"/XML/GameXMLdata.xml";

            var xmlSerializer = new XmlSerializer(locDB.GetType());
            var stream = File.Open(filepath, FileMode.Open);


            locDB = (List<Locations>)xmlSerializer.Deserialize(stream);//locations xmlns=''> was not expected

            stream.Close();

            Debug.Log(locDB[1].Location.Id);

         }

So how do I deserialize XML to List of location objects?那么如何将 XML 反序列化为位置对象列表?

Many thanks for help.非常感谢您的帮助。

You need only two classes:你只需要两个类:

[XmlType("level")]
public class Level
{
    [XmlAttribute("name")]
    public string Name { get; set; }
    [XmlAttribute("complete")]
    public string Complete { get; set; }
    [XmlAttribute("stars")]
    public string Stars { get; set; }
    [XmlAttribute("firstMisson")]
    public string FirstMisson { get; set; }
    [XmlAttribute("secondMission")]
    public string SecondMission { get; set; }
    [XmlAttribute("thridMission")]
    public string ThridMission { get; set; }
}

[XmlType("location")]
public class Location
{
    [XmlElement("level")]
    public Level Level { get; set; }
    [XmlAttribute("id")]
    public string Id { get; set; }
}

The important is to apply XmlType attribute to the class Location rather than XmlRoot .重要的是将XmlType属性应用于类Location而不是XmlRoot

Now you can deserialize your XML to List<Location> like this:现在,您可以像这样将 XML 反序列化为List<Location>

var xmlSerializer = new XmlSerializer(typeof(List<Location>), 
    new XmlRootAttribute("locations"));
List<Location> locations;
using (var stream = File.OpenRead(filepath))
    locations = (List<Location>)xmlSerializer.Deserialize(stream);

The trick is to specify the root element name ("locations" in your case) using the XmlSerializer(Type, XmlRootAttribute) constructor overload.诀窍是使用XmlSerializer(Type, XmlRootAttribute)构造函数重载指定根元素名称(在您的情况下为“位置” XmlSerializer(Type, XmlRootAttribute)

I had a similar problem on a class property and solved it using a different approach.我在类属性上遇到了类似的问题,并使用不同的方法解决了它。 Since in my scenario I wasn't able to touch the XmlSerializer construction logic, this is what I did (using the types from OP for reference):由于在我的场景中我无法触及XmlSerializer构造逻辑,这就是我所做的(使用来自 OP 的类型作为参考):

[XmlRoot("locations")]
public sealed class LocationCollection : Collection<Location>
{
}

With the custom collection class, I'm able to set the root at design time and still use a standard XmlSeriaizer instance.使用自定义集合类,我可以在设计时设置根并仍然使用标准XmlSeriaizer实例。

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

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