2016-08-21 14 views
1

ロケーションオブジェクトのリストにXMLをデシリアライズする際に問題があります。リストにXMLをデシリアライズ<T> - xmlns = ''は期待できませんでした

<?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をデシリアライズん
は、次のXMLを考えると?

多くのおかげで助けてください。

答えて

0

あなたは、2つのクラスだけが必要です。

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

重要なのは、クラスLocationではなくXmlRootXmlType属性を適用することです。

今、あなたは、このようなList<Location>にあなたのXMLをデシリアライズすることができます

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)コンストラクタのオーバーロードを使用して(あなたのケースでは、「場所」)ルート要素名を指定することです。

+0

ありがとう – Slimper

関連する問題