How to serialize xml request posted on WebAPI

167 Views Asked by At

I have two type of request and in both the requests difference is only of the root element so I want to reuse the internal contents these messages are being posted to web API endpoint so they should be serialized automatically but not sure how to achieve this, can anyone help me with this, please?

<Reprocess>
   <request>
     <Title>Mrs</Title>
     <ForeName>Lucy</ForeName>
 </request>
</Reprocess>

<NewApplication>
  <request>
   <Title>Mrs</Title>
   <ForeName>Lucy</ForeName>
  </request>
</NewApplication>
1

There are 1 best solutions below

1
On

First remove root of the xml and than serialize to your internal contents.

            var xmlString1 = "<Reprocess><request><Title>Mrs</Title><ForeName>Lucy</ForeName></request></Reprocess>";
        var xmlString2 = "<NewApplication><request><Title>Mrs</Title><ForeName>Lucy</ForeName></request></NewApplication>";

        XDocument input = XDocument.Load(new StringReader(xmlString1));
        XDocument input2 = XDocument.Load(new StringReader(xmlString2)); ;

        XElement firstChild = input.Root.Elements().First();
        Console.WriteLine(firstChild.ToString());


        XElement firstChild2 = input.Root.Elements().First();
        Console.WriteLine(firstChild2.ToString());

        Console.ReadLine();