0

I am trying to databind in a web api next xml: application/xml to a class

<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>[email protected]</EMAIL>
      </PI>
    </NAME>
  </PERSON>
</COMPANY>

To class:

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; }
            }
        }
    }
}

In controller I have :

[HttpPut]
public HttpResponseMessage GetPerson(COMPANY req) {
...
}

How can I databind it?

I use PostMan to send request

I am getting null in the controller because I think I have to databind class with XML.

1 Answer 1

1

First let's annotate the classes/member with the different 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; }
}

Then we'll tell WebA PI that we want to use the XmlSerializer instead of the DataContractSerializer: Inside the Application_start() method in your Global.asax file add this line:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.