【问题标题】:Adding restrictions to xsd generated from wcf对从 wcf 生成的 xsd 添加限制
【发布时间】:2012-07-01 08:24:05
【问题描述】:

所以我正在研究如何格式化从 WCF 生成的 WSDL 和 XSD,特别是向 WSDL 和 XSD 添加注释/文档以及对各种参数添加限制。

到目前为止,通过创建实现 IWSDLExportExtension 接口和 IOperationBehavior 接口的属性,我已经能够将文档添加到 WSDL 和 XSD。

有关修改架构的一般思路,请参阅:
http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx

有关向 WSDL 添加注释的一般思路,请参阅:
http://msdn.microsoft.com/en-us/library/ms731731(v=vs.110).aspx

但是,当我尝试在 xsd 中为元素添加限制(通过添加简单类型)时遇到了麻烦。

从这里我可以得到一个异常,说明我不能设置元素类型,因为它已经有一个与之关联的只读类型,或者我可以尝试使用只读类型来添加限制,但没有任何反应。

这是生成异常的代码 (System.Xml.Schema.XmlSchemaException: The type attribute cannot be present with either simpleType or complexType.)

    var ComplexType = ((XmlSchemaElement)Schema.Elements[Parameter.XmlQualifiedName]).ElementSchemaType as XmlSchemaComplexType;
    var Sequence = ComplexType.Particle as XmlSchemaSequence;

    foreach (XmlSchemaElement Item in Sequence.Items)
    {
        if (Item.Name = Parameter.Name && Parameter.Length > 0)
        {
            XmlSchemaSimpleType tempType = new XmlSchemaSimpleType();
            XmlSchemaSimpleTypeRestriction tempRestriction = new XmlSchemaSimpleTypeRestriction();
            XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet();
            tempType.Content = tempRestriction;
            tempRestriction.Facets.Add(lengthFacet);
            lengthFacet.Value = "" + Parameter.Length;

            Item.SchemaType = tempType; // <-- Problem code

         }
         ...
     }

这是什么都不做的解决方法:

    var ComplexType = ((XmlSchemaElement)Schema.Elements[Parameter.XmlQualifiedName]).ElementSchemaType as XmlSchemaComplexType;
    var Sequence = ComplexType.Particle as XmlSchemaSequence;

    foreach (XmlSchemaElement Item in Sequence.Items)
    {
        if (Item.Name = Parameter.Name && Parameter.Length > 0)
        {
            XmlSchemaSimpleTypeRestriction tempRestriction = new XmlSchemaSimpleTypeRestriction();
            XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet();
            tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            tempRestriction.Facets.Add(lengthFacet);
            lengthFacet.Value = "" + Parameter.Length;

            // Appears to do nothing
            ((XmlSchemaSimpleType)Item.ElementSchemaType).Content = tempRestriction;

         }
         ...
     }

快速其他说明: 如果我切换到正常的 for 循环并实际尝试用新元素替换问题元素(我知道是 hackish...),我会得到以下异常:System.InvalidCastException: Unable to cast object of type 'System.Xml.Schema.XmlSchemaSimpleType' to type 'System.Xml.Schema.XmlSchemaParticle'. 这个我比较好奇,因为它们都应该是 XmlSchemaElements 对吧?

所以基本上,有谁知道如何向 xmlschemaelements 添加简单类型/添加简单类型限制并让它显示在使用 WCF 的 WSDL 生成的 XSD 中?

谢谢!


编辑: 添加
tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

【问题讨论】:

    标签: .net wcf xsd wsdl restrictions


    【解决方案1】:

    第二个例子,别忘了

    tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
    

    在定义方面之前。限制不是一个单独的模式对象,而是一种派生方法。它从预先存在的简单类型(例如string)派生出您自己的简单类型。

    【讨论】:

    • 感谢您的回复,但简单类型和限制标签仍然没有显示在元素标签下。
    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2018-05-01
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多