C # XML Deserialization with nested nested tags

I know there are several questions on Stackoverflow already on this subject, but I still couldn't find an answer about repeating tags. If I have an XML structure like this:

<root>
    <block>
        <PositionX>100</PositionX>
        <PositionY>100</PositionY>
        <block>
            <PositionX>10</PositionX>
            <PositionY>15</PositionY>
            <button>
            </button>
        </block>
        <button>
        </button>
    </block>
</root>

      

The above structure can have buttons within blocks and blocks within blocks. This is the code I'm currently using that doesn't allow nested elements:

Deserialization:

    public static GUI Deserialize(string filename)
    {
        GUI gui = null;
        XmlSerializer serializer = new XmlSerializer(typeof(GUI));
        StreamReader reader = new StreamReader(filename);
        gui = (GUI)serializer.Deserialize(reader);
        reader.Close();
        return gui;
    }

      

Root class:

[Serializable()]
[XmlRoot("View")]
public class GUI
{
    [XmlArray("Blocks")]
    [XmlArrayItem("Block", typeof(GUIBlock))]
    public GUIBlock[] Blocks { get; set; }
}

      

Block:

[Serializable()]
public class GUIBlock
{
    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("Position")]
    [XmlAttribute("X")]
    public int PositionX { get; set; }

    [XmlElement("Position")]
    [XmlAttribute("Y")]
    public int PositionY { get; set; }

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

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

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

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

    // I also want to allow nested Blocks and Buttons in here, but I **dont** explicitly want to say:
    [XmlArray("Blocks")]
    [XmlArrayItem("Block", typeof(GUIBlock))]
    public GUIBlock[] Blocks { get; set; }
}

      

Is there a way to get an answer that will recursively loop through the element without defining every possible combination?

I do not want to specify a block of blocks and a list of buttons and a button a list of blocks and a list of blocks. And add additional parameters for each new tag.

I could also do this without deserializing and using XPath, but then I don't know the parent / child information unless I research myself. Any help on this?

+3


source to share


3 answers


Didn't find the answer I was looking for because I believe you cannot do this with deserialization. So now my Block class also has a list of blocks, so I can recursively encode them.



0


source


I assume you are asking how can I deserialize an array of objects without creating a ListNode that contains those elements?

For example, you have a requirement that your XML should look like this:

<Library>
    <Location></Location>
    <Book></Book>
    <Book></Book>
    <Book></Book>
</Library>

      

AND CANNOT look like this:



<Library>
    <Location></Location>
    <BookCollection>
       <Book></Book>
       <Book></Book>
       <Book></Book>
    <BookCollection>
</Library>

      

The way you would do it in a C # object looks like this:

[Serializable]
public class Library
{
    [XmlElement]
    public string Location {get;set;}

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

}

public class Book 
{
    /// ....
}

      

+1


source


You need to create a recursive function to go through each node, but what you can do is simply:

// This groups all sub-elements of a node by their name.
// If there is more than one node with the same name, .Count() will return > 1
Int32 numberOfSameNameNodes = 
    node.Elements()
        .GroupBy(element => element.Name)
        .Count(elementsGroupedByName => elementsGroupedByName.Count() > 1);

// if there are duplicately named sub-nodes then 
if (numberOfSameNameNodes > 0)
{
   ...
}

      

And this will determine if the node has sub-nodes that are duplicated.

0


source







All Articles