3

我知道 Stackoverflow 上已经有几个关于这个主题的问题,但我仍然找不到关于重复标签的任何答案。如果我有这样的 XML 结构:

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

上述结构可以有块内的按钮和块内的块。这是我目前使用的代码,它不允许嵌套项:

反序列化:

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

根类:

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

块:

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

有什么方法可以让我得到一个答案,该答案将递归循环该项目而不定义所有可能的组合?

我不想给块一个块列表和一个按钮列表,一个按钮一个块列表和一个块列表。并为每个新标签添加更多选项。

我也可以在没有反序列化的情况下使用 XPath,但是如果我自己不探索它,我不知道有关父/子的信息。对这个有帮助吗?

4

3 回答 3

1

I believe you are asking how can I deserialize an array of objects without having to create a ListNode that contains those items?

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

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

And it CANNOT look like this:

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

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

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

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

}

public class Book 
{
    /// ....
}
于 2013-02-08T17:02:09.803 回答
0

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

// 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 a node has sub-nodes that are duplicates.

于 2013-02-08T16:51:20.073 回答
0

没有找到我正在寻找的答案,因为我相信您无法通过反序列化来做到这一点。所以现在我的 Block 类也有一个 Block 列表,所以我可以递归地循环它们。

于 2013-02-27T15:47:29.757 回答