将根 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> 是我的根并且我只将颜色添加到一个数组中时一切正常。

现在我想通过同一个 XML 文件添加纹理和更多纹理,因此将根文件移动到 <Content>

这就是我的 ColourLoader Class 只有颜色的样子,<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 class 工作正常,它使用 [XmlElement("R")] 等从 XML 读取值。只是我不知道如何从嵌套 XML 中读取元素。 我以某种方式必须跳过 <Content> 并将 <Colours> 中的颜色作为根添加,并对 <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);
    }