【问题标题】:Inserting data into element of a generated class将数据插入到生成的类的元素中
【发布时间】:2014-03-17 21:24:58
【问题描述】:

我已经通过 xsd.exe 工具生成了一个类。

这是架构的相关部分:

<xs:element minOccurs="0" name="propertyLine">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element minOccurs="0" maxOccurs="unbounded" name="value">
                              <xs:complexType mixed="true">
                                <xs:attribute name="name" type="xs:string" />
                              </xs:complexType>
                            </xs:element>
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>

在 xsd 生成类之后,我得到“propertyLine”类型的数组:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class MainSubMessageInfoValue {

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}

所以我通过以下方式将数据设置为名称属性:

propertyLine[0].name = "data";

但现在我想将数据设置到元素 "value"(具有“name”属性的元素)中。

我想要实现的是:

<value name="data">Bla Bla Bla</value>

我怎样才能通过代码做到这一点?如何将“Bla Bla Bla”设置为“value”?

【问题讨论】:

    标签: c# xml xsd


    【解决方案1】:

    您需要一个带有[XmlText] 属性的新属性(属性的名称无关紧要。我已经很久没有处理xsd 语义了,所以我不知道为什么会这样尚未为您生成。添加它的最佳方法是在单独的文件中(因此,生成的类是部分的)。

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 2017-03-11
      • 2023-02-20
      • 2011-12-24
      • 1970-01-01
      • 2018-12-15
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多