当部分验证规则在 WSDL 中时,如何使用 XSD 验证 XML

How validate XML with XSD, when part of validation rules are in WSDL

我使用 C#、.NET 4.5、控制台应用程序。 我在服务参考中添加了 WSDL 文件。 WSDL 内部是验证规则,例如:

<xs:complexType name="xRequest">
  <xs:sequence>
    <xs:element name="SenderDateTime" type="ip:grDateTime"/>
    <xs:element name="SenderId" type="ip:grIdentifier"/>
  </xs:sequence>
</xs:complexType>

还有 XSD 文件,其中包含验证规则的详细信息,例如:

<xs:simpleType name="grDateTime">
    <xs:restriction base="xs:dateTime">
        <xs:pattern value="[0-9]{4,4}\-[0-9]{2,2}\-[0-9]{2,2}[T][0-9]{2,2}:[0-9]{2,2}:[0-9]{2,2}(\.[0-9]{1,6}){0,1}"/>
    </xs:restriction>
</xs:simpleType>

而且我已经在 Reference.cs 中从 WSDL 自动生成属性,例如:

public partial class xRequest
{
    private string senderIdField;
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
    public string SenderId {
        get {
            return this.senderIdField;
        }
        set {
            this.senderIdField = value;
            this.RaisePropertyChanged("SenderId");
        }
    }
}

我将 xRequest 对象序列化为 XML 并且我想验证它。 当部分验证规则在 WSDL 中时,如何使用 XSD 验证 XML?

它并不像它应该的那样直截了当...看看这个 article(第 4、5 和 11 步),我认为这基本上是您想要做的(客户端),但它还展示了如何在服务器端进行验证。