【发布时间】:2012-03-13 15:33:15
【问题描述】:
我有 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>
生成器在消息类中为gameOver创建这个:
/// <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[]'
player 是由生成器定义的类,它在其他属性中起作用。我发现这个片段 xml 只是我有 3 度的复杂节点。
我该如何解决这个问题?
【问题讨论】:
标签: c# .net xml parsing serialization