簡體   English   中英

將有根的XML元素反序列化為數組

[英]Deserialize rooted XML elements into an array

我需要將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>是我的root並且我只在一個Array中添加顏色時,現在一切正常。

現在我想通過相同的XML文件添加Textures以及更多<Content> ,從而將根目錄移動到<Content>

這就是我的ColourLoader類看起來只有Colors和<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>添加Colors作為root,並為<Textures>等做同樣的事情。

我不想創建多個XML文件,因為我希望將所有內容管理到一個位置,並且只加載一次XML文件。

我想這就是你所追求的。 我還包括一個序列化對象的方法。 我發現這對於診斷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);
    }

暫無
暫無

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

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