如何控制 xsi:nill= 上的 XML 序列化行为
我正在开发一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,
我现在明白了!
您必须使用 XmlElementAttribute 显式标记 Quantity 属性!
不知道为什么这没有自动生成......
ok,
I got it now!
You have to mark the Quantity property with the XmlElementAttribute explicitly!
No idea why this hasn't been generated automatically...