gpt4 book ai didi

c# - 从 WSDL 文件读取 minOccurs 和默认值

转载 作者:太空狗 更新时间:2023-10-30 01:12:37 26 4
gpt4 key购买 nike

我正在为 Web 服务的客户端编写代码。在 Web 服务定义文件中,定义了“minOccurs”和默认值。我如何访问这些 minOccurs 和默认值?

在 WSDL 文件中,元素定义如下:

<xs:element minOccurs="0" name="optionalEntry" type="tns:optionalValueType" default="NULL"/>其中 optionalValueType是一个枚举。

Web 服务引用为 webservice和值 NULL可以手动(像任何其他可能的值一样)设置为webservice.optionalValueType.NULL .

最佳答案

过于简单化,WSDL 描述了可由 Web 服务端点使用的文档的 XML 模式。 default 属性是 XML Schema 标准的一部分,该标准规定在未指定其他值时,应自动将此属性中指定的值分配给元素 (https://www.w3schools.com/xml/schema_simple.asp)。

有一些工具可以从给定的 WSDL 文档生成客户端代码。您没有指定您使用的是哪个工具,所以我假设它是 WSDL.exe(Visual Studio 的一部分),但还有其他工具(例如 SoapUI)。因此,您的问题的答案取决于您使用的工具。

严格来说,工具没有义务提供 API 来暴露 default 属性的值。它们应该只生成行为类似于标准中定义的代码,即,如果未指定值,则应使用 default 值。示例:

// Account property is defined like this:
// <s:element minOccurs="0" maxOccurs="1" name="account" type="s:string" default="FOO" />
var connInfo = new ConnectionInfo();
Console.WriteLine(connInfo.account); // Will print "FOO".

回到你的问题,我可以看到以下方法从生成的客户端代码中获取 default 值:

  1. 创建一个类的实例,然后读取字段。这些字段将被初始化为它们的默认值(如上面的代码片段)。
  2. WSDL.exe 将 DefaultValueAttribute 添加到每个具有 default 值的属性。示例(生成的代码):
public partial class ConnectionInfo 
{
private string accountField;

public ConnectionInfo()
{
this.accountField = "FOO";
}

[System.ComponentModel.DefaultValueAttribute("FOO")]
public string account
{
get
{
return this.accountField;
}
set
{
this.accountField = value;
}
}
}

因此应该可以使用一些反射从该属性中获取值:

var type = typeof(ConnectionInfo);
var prop = type.GetProperty("account");
var attr = (DefaultValueAttribute)prop.GetCustomAttributes(
typeof(DefaultValueAttribute), true).First();
Console.WriteLine(attr.Value); // Will print "FOO".

对于 minOccurs 属性,除了自己阅读 WSDL 架构之外,我看不到获取它的方法。

关于c# - 从 WSDL 文件读取 minOccurs 和默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57604501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com