如何在c#中序列化期间为数组列表项提供属性

时间:2014-12-11 05:41:08

标签: c# c#-4.0

我是SOAP Webservice的新手。请建议我如何实现以下XML

我正在尝试生成像这样创建XML片段的C#。

<?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>
 <querySeatResponse xmlns="xxx">
  <querySeatResult>
    <querySeat_status code="int" msg="string">
      <details>
        <detail seat_no="string" available="string" />
        <detail seat_no="string" available="string" />
      </details>
    </querySeat_status>
  </querySeatResult>
 </querySeatResponse>
</soap:Body>
</soap:Envelope>

但我得到以下输出:

<?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>
  <querySeatResponse xmlns="xxx">
   <querySeatResult>
     <querySeat_status code="int" msg="string">
       <details>
         <detail xsi:nil="true" />
         <detail xsi:nil="true" />
       </details>
     </querySeat_status>
   </querySeatResult>
  </querySeatResponse>
 </soap:Body>
</soap:Envelope>

我的源代码如下:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class abc : System.Web.Services.WebService
{
    [WebMethod]
    [SoapDocumentMethod("xxx", RequestNamespace = "xxx", ResponseNamespace = "xxx", Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
    public querySeat_Main querySeat([XmlAttribute] string signature, string operator_code, string route_id, string trip_no, string depart_date, string counter_from, string counter_to, string bus_type)
    {
       querySeat_Main main = new querySeat_Main();
       querySeat_status status = new querySeat_status();
       querySeat_status.detail detail = new querySeat_status.detail();
       status.code = 0;
       status.msg = "success";
       main.querySeat_status = status;
       return main;
    }
}

[Serializable]
[GeneratedCode("System.Xml", "xxxx")]
[XmlType(Namespace = "xxx")]
[DebuggerStepThrough]
[DesignerCategory("code")]
public class querySeat_Main
{
  querySeat_status status;
  public querySeat_Main()
  {
  }
  public querySeat_status querySeat_status { get { return status; } set { status = value; } }
}

[Serializable]
[GeneratedCode("System.Xml", "xxx")]
[XmlType(Namespace = "xxx")]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlRoot("querySeat_status", Namespace = "xxx")]
public class querySeat_status
{
  int Code;
  string Msg;

  public querySeat_status() { }

  [XmlAttribute]
  public int code { get { return Code; } set { Code = value; } }
  [XmlAttribute]
  public string msg { get { return Msg; } set { Msg = value; } }

  [XmlArray("details")]
  [XmlArrayItem("detail")]
  public List<detail> details = new List<detail>();

  [Serializable]
  [GeneratedCode("System.Xml", "xxx")]
  [DebuggerStepThrough]
  [DesignerCategory("code")]
  [XmlRoot("querySeat_status", Namespace = "xxx")]
  public class detail
  {
    string seat;
    string avail;
    public detail() { }

    [XmlAttribute]
    public string seat_no { get { return seat; } set { seat = value; } }
    [XmlAttribute]
    public string available { get { return avail; } set { avail = value; } }
  }
}

1 个答案:

答案 0 :(得分:0)

如Alex所述,看起来您没有初始化您的详细信息,您可以尝试:

public querySeat_Main querySeat([XmlAttribute] string signature, string operator_code, string route_id, string trip_no, string depart_date, string counter_from, string counter_to, string bus_type)
{
    querySeat_Main main = new querySeat_Main();
    querySeat_status status = new querySeat_status();
    querySeat_status.detail detail = new querySeat_status.detail();
    detail.available = "no";
    detail.seat_no = "7";
    status.code = 0;
    status.details = new List<querySeat_status.detail>();
    status.details.Add(detail);
    status.msg = "success";
    main.querySeat_status = status;
    return main;
}