Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查XML中跳过的WCF数据元素?_Wcf_Xsd - Fatal编程技术网

如何检查XML中跳过的WCF数据元素?

如何检查XML中跳过的WCF数据元素?,wcf,xsd,Wcf,Xsd,在我的WCF中,我定义了一个对象产品,如下所示: <xs:complexType name="Product"> <xs:sequence> <xs:element name="Name" type="xs:string"/> <xs:element name="Postcode" type="xs:string" nillable="true" minOccurs="0"/> <xs:

在我的WCF中,我定义了一个对象产品,如下所示:

<xs:complexType name="Product">
    <xs:sequence>
        <xs:element name="Name" type="xs:string"/>
        <xs:element name="Postcode" type="xs:string" nillable="true" minOccurs="0"/>
        <xs:element name="ProductID" type="xs:string" />
    </xs:sequence>
</xs:complexType>
<Product>
    <Name>Product 1</Name>
    <Postcode></Postcode>
    <ProductID>10</ProductID>
</Product>
请注意,元素邮政编码为nillable=true minOccurs=0,这意味着当客户端应用程序调用web服务时,它们可以为该元素提供空值,如下所示:

<xs:complexType name="Product">
    <xs:sequence>
        <xs:element name="Name" type="xs:string"/>
        <xs:element name="Postcode" type="xs:string" nillable="true" minOccurs="0"/>
        <xs:element name="ProductID" type="xs:string" />
    </xs:sequence>
</xs:complexType>
<Product>
    <Name>Product 1</Name>
    <Postcode></Postcode>
    <ProductID>10</ProductID>
</Product>
或者他们可以简单地跳过邮政编码元素,这样传入的邮件就变成了不存在的邮政编码

<Product>
    <Name>Product 1</Name>
    <ProductID>10</ProductID>
</Product>
我的问题是,在宿主程序中,如何检查传入XML消息中是否存在元素邮政编码?我这样问是因为如果它不在那里,我调用该方法来获取Postcode值,那么程序将抛出一个错误:

字符串postcode=product.postcode-此调用将引发错误

非常感谢大家提出的任何想法/建议


Charles

您断言在定义中使用nillable允许您列出的两种情况,这是不正确的

事实上,nillable xsd属性允许构造:

<Postcode xsi:nil='true'/>
是不一样的事情

public class Product
{
}
因此,通过允许调用者构造产品类型的两个不同版本,您实际上被迫定义两个单独的C类型进行反序列化,这意味着您将需要公开服务操作两次,一次用于希望使用带有邮政编码的产品的调用者,另一次用于希望使用不带邮政编码的产品的调用者

我建议您重新定义您的产品类型以包含Postcode元素——毕竟,因为它是一个字符串,如果您的呼叫者不想使用它,可以将其保留为空

PrdocutCode将是一个字符串,可以为空,因此字符串postcode=product.postcode;不应该抛出错误。这是假设您正在从服务获取一个对象,这就是您的代码所暗示的。