用于解析的XML类

时间:2012-03-13 15:33:16

标签: c# .net xml parsing serialization

我有XML结构的类模型,我需要在其中解析xml。 我用过XSD发生器。一切都会好的,但有两件事情不行。

首先:我在xml中有类似的东西:

<protocol>

<!-- an error message which may appear from both sides as a response anytime.-->
<message type="error">
some string
</message>

...
</protocol>

我从xsd生成器获取的类设置在我的消息类中没有任何字段,我可以得到一些字符串。我必须将哪个属性分配给我在该类中创建的字段:(字符串消息)以获取此值?

第二:我在xml中有类似的东西:

<message type="gameState">
<gameId id="zxcc"/>
<!-- one tag of the two below appears in message -->
<nextPlayer nick="asdd"/>
<gameOver>
<!-- this tag appears repeatedly for all the players -->
<player nick="zxc" result="winner"/>
</gameOver>
<!-- this tag will always appear. Not read by the server.-->
<gameState>
</gameState>
</message>

生成器在messageOver的消息类中创建:

/// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute("gameOver", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("player", typeof(player), IsNullable=false)]
    public player[][] gameOver {
        get {
            return this.gameOverField;
        }
        set {
            this.gameOverField = value;
        }
    }

我得到例外:

Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'player[]' to 'player'
error CS0029: Cannot implicitly convert type 'player' to 'player[]'

播放器是由生成器定义的类,它适用于其他属性。我发现这个片段xml只是复杂的节点,我有3度。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

确定。我找到了第一个问题的解决方案。

我不得不添加一个

[XmlText]
public string Value { get; set; }

到我的Message类,在<message></message>中序列化为文本,但我无法找到第二个问题的解决方案。有什么想法吗?