简体   繁体   中英

Deserialize rooted XML elements into an array

I need to save multiple fields from an XML into different arrays. This is what my XML looks like:

<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>

Now everything was working fine when <Colours> was my root and i only added colours into one Array.

Now i want to add Textures and later more through the same XML file, thus moving the root one up to <Content> .

This is what my ColourLoader Class looked like with only Colours and <Colour> being the root of my 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;
        }        
    }
}

My CustomColour class works fine, it uses [XmlElement("R")] etc. to read the values from the XML. It's just that i don't know how to read elements from a nested XML. I somehow have to skip <Content> and add Colours from <Colours> as root and do the same for <Textures> etc.

I don't want to create multiple XML files as i would like to keep all content managing into one spot and only load an XML file once.

I think this is what you're after. I've also included a method that serializes the object. I find this very helpful in diagnosing idiosyncratic issues with XML Serialization...

Please vote as answer if this solved your issue.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM