Asp.net WebAPI XML反序列化?

时间:2017-01-26 16:33:07

标签: c# asp.net xml asp.net-mvc asp.net-web-api

我可以在post post request中成功地在我的ASP.net Web API项目中反序列化XML,

XML:

<Request xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://schemas.datacontract.org/2004/07/TestAPI.Models">
 <Child>
  <CountryISO>
   <country xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d4p1:string>CA</d4p1:string>
    <d4p1:string>US</d4p1:string>
   </country>
  </CountryISO>
 </Child>
</Request>

型号

namespace TestWebTuiAPI.Models
{

 public class Request
  {
   public Trip Child{ get; set; }
  }

  public class Child
   {
    public CountryISO CountryISO { get; set; }
   }

    public class CountryISO
     {
         [XmlElement("country")]
         public List<string> country { get; set; }
     }
 }

现在,我需要执行上述所有步骤,以使我的Post Request在ASP .Net Web API中运行。 如果我从模型中删除[XmlElement(&#34; country&#34;)]属性,XML中的CountryISO标记将返回null或空值。

我想在这里实现的是使用POST请求成功反序列化以下XML,

XML:

<Request>
 <Child>
  <CountryISO>
   <country>CA</country>
   <country>US</country>
  </CountryISO>
 </Child>
</Request>

如果我尝试发布上述XML,我收到的请求无效,并且我收到了空模型。我需要在第一个XML中添加标头(父节点和CountryISO节点)以使其成功运行。我尝试了各种解决方案,但徒劳无功。

任何建议都将不胜感激。

编辑: 如果我使用

config.Formatters.XmlFormatter.UseXmlSerializer = true;

我不需要标题,但我在CountryISO标记内获取空值?

2 个答案:

答案 0 :(得分:1)

如果你想像这样发布xml:

    Set wS = ThisWorkbook.Sheets("SheetName")

注释您的属性/类,如前面的答案所述,还要将Sub testFrankLucas() Dim i As Long Dim LastRow As Long Dim wS As Worksheet Dim wsNew As Worksheet Dim DaTa() As Variant Set wS = ThisWorkbook.Sheets("SheetName") LastRow = LastRow_1(wS) ReDim DaTa(1 To 3, 1 To 1) For i = 1 To LastRow Step 6 With wS DaTa(1, UBound(DaTa, 2)) = .Cells(i, 1).Offset(3, 0) DaTa(2, UBound(DaTa, 2)) = .Cells(i, 1).Offset(4, 0) DaTa(3, UBound(DaTa, 2)) = .Cells(i, 1).Offset(5, 0) ReDim Preserve DaTa(LBound(DaTa, 1) To UBound(DaTa, 1), LBound(DaTa, 2) To UBound(DaTa, 2) + 1) End With 'wS Next i ReDim Preserve DaTa(LBound(DaTa, 1) To UBound(DaTa, 1), LBound(DaTa, 2) To UBound(DaTa, 2) - 1) Set wsNew = ThisWorkbook.Sheets.Add 'wsNew.Range("A1").Resize(UBound(DaTa, 2), UBound(DaTa, 1)).Value = Application.Transpose(DaTa) For i = LBound(DaTa, 2) To UBound(DaTa, 2) For j = LBound(DaTa, 1) To UBound(DaTa, 1) With wsNew .Cells(i, j).Value = DaTa(j, i) End With 'wsNew Next j Next i End Sub Public Function LastRow_1(wS As Worksheet) As Double With wS If Application.WorksheetFunction.CountA(.Cells) <> 0 Then LastRow_1 = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else LastRow_1 = 1 End If End With End Function 属性添加到country属性中:

<Request>
 <Child>
  <CountryISO>
   <country>CA</country>
   <country>US</country>
  </CountryISO>
 </Child>
</Request>

答案 1 :(得分:0)

在WebAPI中,标准做法是分别使用[DataContract]和[DataMember]属性标记序列化操作的类和属性:

namespace TestWebTuiAPI.Models
{
     [DataContract]
     public class Request
     {
         [DataMember]
         public Trip Child{ get; set; }
     }

     [DataContract]
     public class Child
     {
         [DataMember]
         public CountryISO CountryISO { get; set; }
     }

     [DataContract]
     public class CountryISO
     {
         [DataMember]
         public List<string> country { get; set; }
     }
 }

如果没有适当的属性,WebAPI不知道它应该序列化您的属性。