I'm working on a service in which I pass a JSON request, I need to convert it to XML and them compare it to some XML schemas and then do something with it.
My problem is with the attributes. For example, my JSON request is:
{
"BookingSearch_IN": {
"MsgHeader": {
"MessageID": "ABC010101",
"ContextRecord": {
"ContextInfo": {
"ItineraryDetails": {
"ItinerarySeqNmbr": "1",
"StartDate": "2017-04-01",
"EndDate": "2017-04-14",
"LocationStart": {
"LocationContext": "AIRPORT",
"LocationCode": "MIA"
},
"LocationEnd": {
"LocationContext": "AIRPORT",
"LocationCode": "MIA"
}
},
"ExtraInfoList": {
"ExtraInfo": {
"Category": "CRUISE",
"Item": {
"Code": "SHIP_CODE",
"Value": "MAGIC"
}
}
},
"_ResType": "Vacation"
}
}
},
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"_xmlns:cs": "http://MyCompany.com",
"_version": "6.66",
"__prefix": "cs"
}
}
I'm using: XmlDocument doc = JsonConvert.DeserializeXmlNode(jsonObject);
to convert it to XML. The xml result is:
<BookingSearch_IN>
<MsgHeader>
<MessageID>ABC010101</MessageID>
<ContextRecord>
<ContextInfo>
<ItineraryDetails>
<ItinerarySeqNmbr>1</ItinerarySeqNmbr>
<StartDate>2017-04-01</StartDate>
<EndDate>2017-04-14</EndDate>
<LocationStart>
<LocationContext>AIRPORT</LocationContext>
<LocationCode>MIA</LocationCode>
</LocationStart>
<LocationEnd>
<LocationContext>AIRPORT</LocationContext>
<LocationCode>MIA</LocationCode>
</LocationEnd>
</ItineraryDetails>
<ExtraInfoList>
<ExtraInfo>
<Category>CRUISE</Category>
<Item>
<Code>SHIP_CODE</Code>
<Value>MAGIC</Value>
</Item>
</ExtraInfo>
</ExtraInfoList>
<_ResType>Vacation</_ResType>
</ContextInfo>
</ContextRecord>
</MsgHeader>
<xsi>http://www.w3.org/2001/XMLSchema-instance</xsi><cs>http://MyCompany.com</cs><_version>6.66</_version><__prefix>cs</__prefix>
</BookingSearch_IN>
The result XML has the attributes of the root element at the end of the document, as another elements (between </MsgHeader> and </BookingSearch_IN>)
. My problem is that the service that validates this XML against other customers' XML checks these attributes in the root element. Here is how the code expects the XML to be:
<?xml version="1.0" encoding="UTF-8"?>
<cs:BookingSearch_IN xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cs="http://MyCompany.com" version="6.66">
<MsgHeader>
<MessageID>ABC010101</MessageID>
<ContextRecord>
<ContextInfo ResType="Vacation">
<ItineraryDetails>
<ItinerarySeqNmbr>1</ItinerarySeqNmbr>
<StartDate>2017-04-01</StartDate>
<EndDate>2017-04-14</EndDate>
<LocationStart>
<LocationContext>AIRPORT</LocationContext>
<LocationCode>MIA</LocationCode>
</LocationStart>
<LocationEnd>
<LocationContext>AIRPORT</LocationContext>
<LocationCode>MIA</LocationCode>
</LocationEnd>
</ItineraryDetails>
<ExtraInfoList>
<ExtraInfo>
<Category>CRUISE</Category>
<Item>
<Code>SHIP_CODE</Code>
<Value>MAGIC</Value>
</Item>
</ExtraInfo>
</ExtraInfoList>
</ContextInfo>
</ContextRecord>
</MsgHeader>
</cs:BookingSearch_IN>
The code fails because it expect the root element to be <cs:BookingSearch_IN xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cs="http://MyCompany.com" version="5.22">
.... any advise on how to handle this situation? or how to put those attributes back to where they belong in the root element?