0

How do I create a .net object that will serialise to the following using web api

<xml>
<merchant>
<product></product>
<uri></uri>
<product></product>
<uri></uri>
<product></product>
<uri></uri>
</merchant>
1
  • You don't - thats not valid xml for serialization/deserialization to a list of objects Commented Feb 14, 2017 at 17:00

1 Answer 1

1

Use this site to generate your C# object: http://xmltocsharp.azurewebsites.net/

Use the XmlSerializer to serialize or deserialize into an Object.

XmlSerializer serializer = new XmlSerializer(typeof(YourType));

// From XML
using(StreamReader reader = new StreamReader(""))
{
    var yourTypeCollection = (Person)serializer.Deserialize(reader);
}

// To XML
using(StringWriter writer = new StringWriter())
{
    xmlSerializer.Serialize(writer, toSerialize);
    return writer.ToString();
}    
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry the output from this wouldn't work, it gives 2 list of product and uri but the spec says that the uri should follow the product tag. Crazy I know but that's what they expect.
If they are using non-standard serialization rules, then it gets a bit ugly. Typically, you'll have to write your own plugin or code to generate the string at that point. I wish I had better news.
There might be a solution, if you alter the generated object slightly. Let me add a sample.

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.