Background Information
I have two .net services (say A and B). Service B uses a service reference of Service A. Here, 'basicHttpBinding' is being used.
There is a global.asax.cs present in Service A where I plan to perform some operations before the call is sent to Service A.svc.cs
I'm able to read request body in global.asax.cs using the following code.
StreamReader streamReader = new StreamReader(HttpContext.Current.Request.InputStream);
streamReader.BaseStream.Position = 0;
string message = streamReader.ReadToEnd();
The 'message' string variable holds the request body i.e. payload in xml format. I'm able to read the xml using the following code.
XmlDocument doc = new XmlDocument();
doc.LoadXml(message);
The xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<FunctionName xmlns="http://tempuri.org/">
<sampleString>value</sampleString>
<sampleObject xmlns:a="http://schemas.datacontract.org/2004/07/contract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:sampleProperty1>value1</a:sampleProperty1>
<a:sampleProperty2>value2</a:sampleProperty2>
</sampleObject>
</FunctionName>
</s:Body>
</s:Envelope>
Question
Is there any way to convert this xml to json? I'm only interested in the data inside in the xml.
Bonus Question
What does 'a:' in 'a:sampleProperty' mean / stand for?
Desired Output
The final json should like this
{
"sampleString": "value",
"sampleObject": {
"sampleProperty1": "value1",
"sampleProperty2": "value2"
}
}
Things that I have tried
I have tried removing top parent nodes and their attributes using code. Then, I used to JsonConvert to convert xml to json
JsonConvert.SerializeXmlNode(doc.ChildNodes[0].ChildNodes[0].ChildNodes[0], Newtonsoft.Json.Formatting.None, true);
Doing this only helped me partially and I ended with the following json output
{
"sampleString": "value",
"sampleObject": {
"@xmlns:a":"http://schemas.datacontract.org/2004/07/contract",
"@xmlns:i":"http://www.w3.org/2001/XMLSchema-instance",
"a:sampleProperty1": "value1",
"a:sampleProperty2": "value2"
}
}