2015-02-23 47 views
3

我需要將XML中的多個字段保存到不同的數組中。 這是我的XML是什麼樣子:將有根元素的XML元素反序列化到數組中

<Content> 
    <Colours> 
     <Colour name="Strong Red"> 
       <R>255</R>  
       <G>0</G> 
       <B>0</B> 
       <A>255</A> 
     </Colour> 
    </Colours> 
    <Textures> 
     <Texture name="Character01"> 
      <Path>Path/Folder</Path> 
     </Texture> 
    </Textures> 
</Content> 

現在一切工作正常,當<Colours>是我的根,我只加了顏色成一個陣列。

現在我想通過相同的XML文件添加紋理,然後更多,從而將根移動到<Content>

這是我的ColourLoader類看上去就像只有顏色和<Colour>是我的XML的根:

[Serializable()] 
[XmlRoot("Colours")] 
public class ColourLoader 
{ 
    [XmlElement("Colour")] 
    public CustomColour[] Colours; 

    public static ColourLoader Load(string path) 
    { 
     var serializer = new XmlSerializer(typeof(ColourLoader)); 
     using (var stream = new FileStream(path, FileMode.Open)) 
     { 
      return serializer.Deserialize(stream) as ColourLoader; 
     }   
    } 
} 

我CustomColour級工作正常,它使用[XmlElement("R")]等從XML讀出值。只是我不知道如何從嵌套的XML中讀取元素。 我無論如何都跳過<Content><Colours>添加顏色爲根,做同樣的<Textures>

我不想創建多個XML文件,因爲我想保留的所有內容管理在一處,只加載一次XML文件。

+1

創建一個Content類併爲其添加一個屬性Colours。 – EZI 2015-02-23 16:46:13

+1

你打算從同一個XML文檔中加載單獨的'* Loader'類('ColourLoader','TextureLoader'等)嗎? – Jamiec 2015-02-23 16:51:40

+0

這就是它目前的設置,是的。我寧願將內容加載到一個類中,但這是我第一次使用Xml。現在設置的方式就是各種嘗試的結果。 – Rynji 2015-02-23 17:10:46

回答

3

我認爲這是你所追求的。我還包括一個序列化對象的方法。我發現這對診斷XML序列化的特殊問題非常有幫助...

如果這解決了您的問題,請投票作爲答案。

[Serializable()] 
    [XmlRoot("Content")] 
    public class Content 
    { 
     [XmlArray("Colours")] 
     [XmlArrayItem("Colour")] 
     public CustomColour[] Colours { get; set; } 

     [XmlArray("Textures")] 
     [XmlArrayItem("Texture")] 
     public CustomTexture[] Textures { get; set; } 
    } 

    [Serializable()] 
    [XmlRoot("Colour")] 
    public class CustomColour 
    { 
     [XmlAttribute("name")] 
     public string Name { get; set; } 

     [XmlElement("R")] 
     public int R { get; set; } 

     [XmlElement("G")] 
     public int G { get; set; } 

     [XmlElement("B")] 
     public int B { get; set; } 

     [XmlElement("A")] 
     public int A { get; set; } 
    } 

    [Serializable()] 
    [XmlRoot("Texture")] 
    public class CustomTexture 
    { 
     [XmlAttribute("name")] 
     public string Name { get; set; } 

     [XmlElement("Path")] 
     public string Path { get; set; } 
    } 

    public static class ContentLoader 
    { 
     public static Content Load(TextReader textReader) 
     { 
      var serializer = new XmlSerializer(typeof(Content)); 
      var ret = serializer.Deserialize(textReader) as Content; 
      return ret; 
     } 

     public static void Save(TextWriter textWriter, Content content) 
     { 
      var serializer = new XmlSerializer(typeof(Content)); 
      serializer.Serialize(textWriter, content); 
     } 
    } 

    public static void XmlSerializing() 
    { 
     var xml = @"<?xml version=""1.0"" encoding=""utf-16""?> 
        <Content xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> 
         <Colours> 
          <Colour name=""Strong Red""> 
            <R>255</R>  
            <G>0</G> 
            <B>0</B> 
            <A>255</A> 
          </Colour> 
         </Colours> 
         <Textures> 
          <Texture name=""Character01""> 
           <Path>Path/Folder</Path> 
          </Texture> 
         </Textures> 
        </Content>"; 
     var reader = new StringReader(xml); 
     var content = ContentLoader.Load(reader); 

     Console.WriteLine("Deserialized version:"); 
     Console.WriteLine(" Colours"); 
     foreach (var colour in content.Colours) 
     { 
      Console.WriteLine(" R: {0}, G: {1}, B: {2}, A: {3}", colour.R, colour.G, colour.B, colour.A); 
     } 

     Console.WriteLine(" Textures"); 
     foreach (var texture in content.Textures) 
     { 
      Console.WriteLine(" Path: {0}", texture.Path); 
     } 

     var contentObj = new Content() 
          { 
           Colours = new[] { new CustomColour() { Name = "StrongRed", R = 255, G = 0, B = 0, A = 255 } }, 
           Textures = new[] { new CustomTexture() { Name = "Character01", Path = "Path/Folder" } } 
          }; 

     Console.WriteLine(string.Empty); 
     Console.WriteLine("Serialized version:"); 
     var writer = new StringWriter(); 
     ContentLoader.Save(writer, contentObj); 
     Console.WriteLine(writer); 
    } 
+0

它工作完美,非常感謝。我現在可以添加儘可能多的自定義內容元素,並且都可以完美地加載到遊戲中。 – Rynji 2015-02-24 10:07:39