我正在处理一个我传递JSON请求的服务,我需要将它转换为XML,然后将它与一些XML模式进行比较,然后对它做一些事情。
我的问题在于属性。例如,我的JSON请求是:
{
"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"
}
}
我正在使用:XmlDocument doc = JsonConvert.DeserializeXmlNode(jsonObject);
将其转换为XML。 xml结果是:
<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>
结果XML具有文档末尾的根元素的属性,作为另一个元素(between </MsgHeader> and </BookingSearch_IN>)
。我的问题是,针对其他客户的XML验证此XML的服务会检查根元素中的这些属性。以下是代码期望XML的方式:
<?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>
代码失败,因为它期望根元素为<cs:BookingSearch_IN xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cs="http://MyCompany.com" version="5.22">
....有关如何处理这种情况的任何建议?或者如何将这些属性放回到它们在根元素中的位置?
答案 0 :(得分:2)
NewtonSoft期望属性以@
为前缀,而不是_
或__
,并且期望前缀只能被添加到名称中。例如,如果你可以让你的JSON看起来像这样:
{
"cs:BookingSearch_IN": {
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:cs": "http://MyCompany.com",
"@version": "6.66",
"MsgHeader": {
"MessageID": "ABC010101",
"ContextRecord": {
"ContextInfo": {
"@ResType": "Vacation",
"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"
}
}
}
}
}
}
}
}
它将正常工作(请注意,您必须将这些XMLNS移到顶部 - 如果它们位于底部,则会因某种原因丢弃前缀)。
如果不可能,您必须使用不同的序列化程序或编写一些自定义逻辑来预处理/后处理您的JSON或XML。