簡體   English   中英

將 Xml 反序列化為列表<T> - xmlns=&#39;&#39; 不是預期的

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

我在將 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>

以及以下課程:

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

反序列化方法

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

         }

那么如何將 XML 反序列化為位置對象列表?

非常感謝您的幫助。

你只需要兩個類:

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

重要的是將XmlType屬性應用於類Location而不是XmlRoot

現在,您可以像這樣將 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);

訣竅是使用XmlSerializer(Type, XmlRootAttribute)構造函數重載指定根元素名稱(在您的情況下為“位置” XmlSerializer(Type, XmlRootAttribute)

我在類屬性上遇到了類似的問題,並使用不同的方法解決了它。 由於在我的場景中我無法觸及XmlSerializer構造邏輯,這就是我所做的(使用來自 OP 的類型作為參考):

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

使用自定義集合類,我可以在設計時設置根並仍然使用標准XmlSeriaizer實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM