0

I consume a third-party web service that uses SOAP XML. I construct my XML request and get XML back like this:

<GetCustomers xmlns="">
  <Header>
    <Version>1.0</Version>
    <Type>GetCustomers</Type>
    <GeneratedDate>07/11/2013 13:30</GeneratedDate>
  </Header>
  <ResultMessage>
    <Message>7 Customers found</Message>
    <Code>CustomerFound</Code>
    <Success>true</Success>
  </ResultMessage>
  <Customer>
    <CustomerName>CUSTOMER ABC</CustomerName>
    <CustomerID>33</CustomerID>
    <CustomerCode></CustomerCode>
  </Customer>

It returns it as an object type XmlNode and I want to convert this response into a ViewModel I have created as part of an MVC 4 app I am building. What is an approach that will work?

1 Answer 1

1

Convert the full XML into a string first:

string xml = response.OuterXml.ToString();

Then use XDocument:

XDocument xDoc = XDocument.Parse(xml);

Now use LINQ to XML to select each Customer element and create a list:

var obj = (from element in xDoc.Descendants("GetCustomers").Elements("Customer")
           select new {
              ID = element.Element("CustomerID").Value,
              Name element.Element("CustomerName").Value,
              Code = element.Element("CustomerCode").Value
           }).ToList();

I am sure there are many better ways to now convert this into my ViewModel data but this is how I did it:

foreach (var item in obj)
{
    RMCustomer customer = new RMCustomer();
    if (item.ID != null && item.ID.Length > 0)
    {
        customer.RMInternalUniqueID = Convert.ToInt32(item.ID);
    }

    if (item.Code != null && item.Code.Length > 0)
    {
        customer.CustomerRMExternalID = Convert.ToInt32(item.Code);
    }
    else
    {
        customer.CustomerRMExternalID = null;
    }
    if (item.Name != null && item.Name.Length > 0)
    {
        customer.CustomerName = item.Name;
    }
    if (customer != null)
    {
        roadMarqueCustomers.Add(customer);
    }
}

I should point out my ViewModel is simply:

public class RMCustomer
{
    public string CustomerName { get; set; }
    public int CustomerRMInternalUniqueID { get; set; }
    public Nullable<int> CustomerRMExternalID { get; set; }
}

Finally:

 return roadMarqueCustomers;
Sign up to request clarification or add additional context in comments.

8 Comments

If you knew the answer, why did you ask the question?
Because I was searching for ages to find an answer and couldn't get various other methods to work so I thought I'd share what I found in case it helps other newbies like me. I thought it was within the rules as per this: stackoverflow.com/help/self-answer
@MikeRouse Just a quick hint to help you - instead of doing someString != null && someString.Length > 0 you can do String.IsNullOrEmpty(someString) or if any of null, " ", " ", " ", etc is not wanted - String.IsNullOrWhiteSpace(someString)
Also, you could use XMLSerializer class to perform the actual mapping from XML, I have an example here: stackoverflow.com/a/19448271/1362136
Or you can let someone else do all the lifting, including the consuming of the web-service with something like ServiceStack. You create the DTO's (Data Transfer Objects) for the service and it consumes the web-service and hands you your DTO's.
|

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.