首页 文章

如何为Web服务声明复杂的嵌套C#类型

提问于
浏览
1

我想创建一个接受复杂嵌套类型的服务 . 在我创建的示例asmx文件中:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class ServiceNest : System.Web.Services.WebService
{
   public class Block
   {
      [XmlElement(IsNullable = false)]
      public int number;
   }

   public class Cell
   {
      [XmlElement(IsNullable = false)]
      public Block block;
   }

   public class Head
   {
      [XmlElement(IsNullable = false)]
      public Cell cell;
   }

   public class Nest
   {
      public Head head;
   }

   [WebMethod]
   public void TakeNest(Nest nest)
   {
   }

}

当我在IE中查看asmx文件时,测试页面将示例SOAP post请求显示为:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <TakeNest xmlns="http://schemas.intellicorp.com/livecompare/">
      <nest>
        <head>
          <cell>
            <block xsi:nil="true" />
          </cell>
        </head>
      </nest>
    </TakeNest>
  </soap:Body>
</soap:Envelope>

它没有将<block>扩展为其编号成员 .

看一下WSDL,这些类型看起来都很好 . 那么这只是后期演示页面创建者的限制吗?

谢谢 .

3 回答

  • 1

    但那些元素 ARE 为null . 你需要在它们出现之前构造它们,否则它们只是空的 .

  • 0

    正如Kevin指出的那样,POST XML表明这些元素是零 . 我应该只是尝试使用Web服务 . 一旦我这样做,我可以看到导入器(.NET,Java或Ruby)正确创建了所有类型 . 毕竟,这里真的没有问题 .

  • 0

    .NET代码在一定数量的级别后没有放弃 .

    如果查看"Add Web Reference"生成的代码,则'll find that there'是 bool numberSpecified 字段 . 仅当客户端将其设置为 true 时,才会序列化该号码 .

    如果查看XML Schema,您会看到 number 元素可能不存在 . 如果它是引用类型,则可以通过 null 值在客户端中表示 . 由于它是 int ,因此需要此附加标志来指示是否序列化此可选值 .

相关问题