我正在尝试将web api中的xml:application / xml数据绑定到类
<COMPANY>
<PROPERTIES>
<DATASOURCE>UXXI</DATASOURCE>
<DATETIME>2012-07-11T11:18:12</DATETIME>
</PROPERTIES>
<PERSON recstatus="1">
<SOURCEDID>
<SOURCE>U123XXI</SOURCE>
<ID>usuario26</ID>
</SOURCEDID>
<USERID password="123456">usuario26</USERID>
<NAME>
<FN>Usuario Prueba 26</FN>
<PI>
<FAMILY>Prueba 26</FAMILY>
<GIVEN>Usuario</GIVEN>
<EMAIL>usuario26@ocu.es</EMAIL>
</PI>
</NAME>
</PERSON>
</COMPANY>
上课:
public class COMPANY {
public class PROPERTIES {
public string DATASOURCE { get; set; }
public string MYDATE { get; set; }
}
public class PERSON {
public string recstatus { get; set; }
public class SOURCEID {
public string SOURCE { get; set; }
public string ID { get; set; }
}
public class USER {
public string password { get; set; }
public string USERID { get; set; }
}
public class NAME {
public string FN {set; get;}
public class PI {
public string FAMILY { set; get; }
public string GIVEN { set; get; }
public string EMAIL { set; get; }
}
}
}
}
在控制器中我有:
[HttpPut]
public HttpResponseMessage GetPerson(COMPANY req) {
...
}
我怎样才能对它进行数据处理?
我使用PostMan发送请求
我在控制器中变为null,因为我认为我必须使用XML来数据类。
答案 0 :(得分:1)
首先,让我们使用不同的ElementName
/ AttributeName
注释类/成员:
[XmlRoot(ElementName = "PROPERTIES")]
public class Properties
{
[XmlElement(ElementName = "DATASOURCE")]
public string Datasource { get; set; }
[XmlElement(ElementName = "DATETIME")]
public string Datetime { get; set; }
}
[XmlRoot(ElementName = "SOURCEDID")]
public class Sourcedid
{
[XmlElement(ElementName = "SOURCE")]
public string Source { get; set; }
[XmlElement(ElementName = "ID")]
public string ID { get; set; }
}
[XmlRoot(ElementName = "USERID")]
public class UserId
{
[XmlAttribute(AttributeName = "password")]
public string Password { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "PI")]
public class PI
{
[XmlElement(ElementName = "FAMILY")]
public string Family { get; set; }
[XmlElement(ElementName = "GIVEN")]
public string Given { get; set; }
[XmlElement(ElementName = "EMAIL")]
public string Email { get; set; }
}
[XmlRoot(ElementName = "NAME")]
public class Name
{
[XmlElement(ElementName = "FN")]
public string FN { get; set; }
[XmlElement(ElementName = "PI")]
public PI PI { get; set; }
}
[XmlRoot(ElementName = "PERSON")]
public class Person
{
[XmlElement(ElementName = "SOURCEDID")]
public Sourcedid Sourcedid { get; set; }
[XmlElement(ElementName = "USERID")]
public UserId Userid { get; set; }
[XmlElement(ElementName = "NAME")]
public Name Name { get; set; }
[XmlAttribute(AttributeName = "recstatus")]
public string Recstatus { get; set; }
}
[XmlRoot(ElementName = "COMPANY")]
public class Company
{
[XmlElement(ElementName = "PROPERTIES")]
public Properties Properties { get; set; }
[XmlElement(ElementName = "PERSON")]
public Person Person { get; set; }
}
然后我们告诉WebA PI我们要使用XmlSerializer
而不是DataContractSerializer
:在Global.asax文件中的Application_start()
方法内添加以下行:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;