【问题标题】:XML class for parse用于解析的 XML 类
【发布时间】: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


    【解决方案1】:

    好的。我为我的第一个问题找到了解决方案。

    我必须添加一个

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

    到我的 Message 类中将其序列化为 &lt;message&gt;&lt;/message&gt; 中的文本,但我找不到第二个问题的解决方案。有什么想法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-28
      • 2013-07-31
      • 1970-01-01
      • 2013-08-26
      • 2010-12-27
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      相关资源
      最近更新 更多