如何控制 xsi:nill= 上的 XML 序列化行为

发布于 2024-07-14 20:00:05 字数 2452 浏览 10 评论 0原文

我正在开发一个 Web 服务来在 2 个 ERP 系统之间共享数据。 第一个 ERP 调用 Web 服务,该服务序列化数据对象并将其发送到第二个 ERP。

数据对象如下所示:

    <xs:complexType name="Parent">
        <xs:sequence>
            <xs:element ref="ta:ReceiptLine" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Child">
        <xs:sequence>
            ...
            <xs:element name="SerialNo" type="xs:string" nillable="true" minOccurs="0"/>
            <xs:element name="Quantity" type="xs:int" nillable="false"/>
            ...
        </xs:sequence>
    </xs:complexType>
    ...
    <xs:element name="Child" type="ta:Child" nillable="true"/>

XSD 生成的类:

[System.Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=false)]
public partial class Parent {
    private Child[] child;

    [System.Xml.Serialization.XmlElementAttribute("Child", IsNullable=true)]
        public Child[] Child {
            get {return this.child;}
            set {this.child = value;}
}

[System.Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=true)]
    public partial class Child{
        private string serialNo;
        private int quantity;

        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string SerialNo {
            get {return this.serialNo;}
            set {this.serialNo = value;}
        }

        public int Quantity {
            get { return this.quantity;}
            set {this.quantity = value;}
        }
}

我正在使用 XmlSerializer 序列化我的数据对象

问题是:(在序列化时)每次当子对象为空时(xsi: nil="true") XSD 无论如何都会生成整个子结构。 并且因为 Quantity 不可为空/可为空,XSD 将 0 写入值...像这样:

<Parent>
  <Child xsi:nil="true">
    <SerialNo xsi:nil="true" />
    <Quantity>0</Quantity>
  </Child>
</Parent>

我期望得到这样的结果:

<Parent>
  </Child xsi:nil="true">
</Parent>

问题是:有没有办法阻止 XSD 解析 xsi:nil="true"-Object ??

有什么建议么?

TIA

I'm working on a Webservice to share data between 2 ERP-systems. First ERP calls the webservice, which serializes the data-object and sends it to the second ERP.

A data object looks like this:

    <xs:complexType name="Parent">
        <xs:sequence>
            <xs:element ref="ta:ReceiptLine" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Child">
        <xs:sequence>
            ...
            <xs:element name="SerialNo" type="xs:string" nillable="true" minOccurs="0"/>
            <xs:element name="Quantity" type="xs:int" nillable="false"/>
            ...
        </xs:sequence>
    </xs:complexType>
    ...
    <xs:element name="Child" type="ta:Child" nillable="true"/>

The classes generated by XSD:

[System.Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=false)]
public partial class Parent {
    private Child[] child;

    [System.Xml.Serialization.XmlElementAttribute("Child", IsNullable=true)]
        public Child[] Child {
            get {return this.child;}
            set {this.child = value;}
}

[System.Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=true)]
    public partial class Child{
        private string serialNo;
        private int quantity;

        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string SerialNo {
            get {return this.serialNo;}
            set {this.serialNo = value;}
        }

        public int Quantity {
            get { return this.quantity;}
            set {this.quantity = value;}
        }
}

I'm serializing my data objects with XmlSerializer

The Problem Is: (On serialization) Every time in case of the Child object is empty (xsi:nil="true") XSD generates the whole Child structure anyway. And because Quantity is not nillable/nullable XSD writes 0 as value... Like this:

<Parent>
  <Child xsi:nil="true">
    <SerialNo xsi:nil="true" />
    <Quantity>0</Quantity>
  </Child>
</Parent>

I expected to get something like this:

<Parent>
  </Child xsi:nil="true">
</Parent>

The Question Is: Is there a way to prevent XSD from parsing an xsi:nil="true"-Object ??

Any suggestions?

TIA

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

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

发布评论

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

评论(1

可可 2024-07-21 20:00:05

好的,

我现在明白了!
您必须使用 XmlElementAttribute 显式标记 Quantity 属性!

[XmlElement(IsNullable=false)]
public int Quantity {
        get { return this.quantity;}
        set {this.quantity = value;}
    }

不知道为什么这没有自动生成......

ok,

I got it now!
You have to mark the Quantity property with the XmlElementAttribute explicitly!

[XmlElement(IsNullable=false)]
public int Quantity {
        get { return this.quantity;}
        set {this.quantity = value;}
    }

No idea why this hasn't been generated automatically...

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