将有根的XML元素反序列化为数组

时间:2015-02-23 16:41:28

标签: c# arrays xml object serialization

我需要将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文件添加Textures以及更多内容,从而将根目录移到<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>为根添加颜色,并对<Textures>等执行相同操作。

我不想创建多个XML文件,因为我希望将所有内容管理到一个位置,并且只加载一次XML文件。

1 个答案:

答案 0 :(得分: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);
    }