XSD 没有按照我的预期生成 C# 类

发布于 2024-11-08 06:26:25 字数 3919 浏览 0 评论 0原文

我有以下 xml 文件:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header xmlns:hdr="http://xml.example.com/ns/appmw/soap/1.0/header">
    <hdr:FinalMessage>true</hdr:FinalMessage>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <MyResponse>
      <Result seqNo="0">
        <MetaData>
          <DataSchema>
            <ColumnSchema type="char" ref="item1" name="Item1"/>
            <ColumnSchema type="char" ref="item2" name="Item2"/>
            <ColumnSchema type="char" ref="item3" name="Item3"/>
          </DataSchema>
        </MetaData>
        <Data>
          <Item>
            <Column ref="item1">Foo</Column>
            <Column ref="item2">Bar</Column>
            <Column ref="item3">Baz</Column>
          </Item>
          <Item>
            <Column ref="item1">Foo</Column>
            <Column ref="item2">Bar</Column>
            <Column ref="item3">Baz</Column>
          </Item>
        </Data>
      </Result>
    </MyResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用 xsd,我生成了 xsd 文件,然后从这些文件生成了 C# 类。它有效并且反序列化很好,但是生成的数据部件类不是我所期望的。

这是相关的 xsd 部分:

      <xs:element name="Data" minOccurs="0" maxOccurs="1">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Item" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Column" nillable="true" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:simpleContent msdata:ColumnName="Column_Text" msdata:Ordinal="1">
                        <xs:extension base="xs:string">
                          <xs:attribute name="ref" form="unqualified" type="xs:string" />
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>

将有一个数据部分,可以包含可变数量的项目。每个项目可以有可变数量的列。

以下是为该类生成的内容:

private MyResponseResultItemColumn[][] dataField;

    [System.Xml.Serialization.XmlArrayItemAttribute("Item", IsNullable=false)]
    [System.Xml.Serialization.XmlArrayItemAttribute("Column", NestingLevel=1)]
    public MyResponseResultItemColumn[][] Data {
        get {
            return this.dataField;
        }
        set {
            this.dataField = value;
        }
    }


[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class MyResponseResultItemColumn {

    private string refField;

    private string valueField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

如您所见,xsd 将 Data 制作为数组的数组(ItemColumn 数组的数组)。我期待并希望看到的是 Data 作为 Item 类的简单数组,其中 Item 类包含 ItemColumns 数组作为属性。

有什么方法可以实现这个目标吗?我希望能够键入 MyResponse.Result.Data[0].ItemColumns,而不是返回 ItemColumn 数组。

I have the following xml file:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header xmlns:hdr="http://xml.example.com/ns/appmw/soap/1.0/header">
    <hdr:FinalMessage>true</hdr:FinalMessage>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <MyResponse>
      <Result seqNo="0">
        <MetaData>
          <DataSchema>
            <ColumnSchema type="char" ref="item1" name="Item1"/>
            <ColumnSchema type="char" ref="item2" name="Item2"/>
            <ColumnSchema type="char" ref="item3" name="Item3"/>
          </DataSchema>
        </MetaData>
        <Data>
          <Item>
            <Column ref="item1">Foo</Column>
            <Column ref="item2">Bar</Column>
            <Column ref="item3">Baz</Column>
          </Item>
          <Item>
            <Column ref="item1">Foo</Column>
            <Column ref="item2">Bar</Column>
            <Column ref="item3">Baz</Column>
          </Item>
        </Data>
      </Result>
    </MyResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Using xsd, I generated xsd files and then a C# class from those files. It works and the deserialization is fine, but the Data part class that was generated is not what I expected.

Here is the relevant xsd section:

      <xs:element name="Data" minOccurs="0" maxOccurs="1">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Item" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Column" nillable="true" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:simpleContent msdata:ColumnName="Column_Text" msdata:Ordinal="1">
                        <xs:extension base="xs:string">
                          <xs:attribute name="ref" form="unqualified" type="xs:string" />
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>

There will be a single Data section that can have a variable number of Items. Each Item can have a variable number of Columns.

Here is what was generated for the class:

private MyResponseResultItemColumn[][] dataField;

    [System.Xml.Serialization.XmlArrayItemAttribute("Item", IsNullable=false)]
    [System.Xml.Serialization.XmlArrayItemAttribute("Column", NestingLevel=1)]
    public MyResponseResultItemColumn[][] Data {
        get {
            return this.dataField;
        }
        set {
            this.dataField = value;
        }
    }


[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class MyResponseResultItemColumn {

    private string refField;

    private string valueField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

As you can see, xsd made Data an array of arrays (array of ItemColumn arrays). What I was expecting and want to see is Data as simple array of an Item class, where the Item class contains an array of ItemColumns as a property.

Is there a way I can achieve this? Instead of MyResponse.Result.Data[0][0] returning an array of ItemColumn, I want to be able to type MyResponse.Result.Data[0].ItemColumns.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2024-11-15 06:26:26

You can write your own serializer by implementing the IXmlSerializable interface. It's a bit troublesome, but you have complete control over the shape of the object. This is a pretty good article on how to implement it, which I found in a thread on stackoverflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文