在 C# 中将 JSON 转换为 XML 时出现问题处理属性

Issue handling attributes when converting JSON to XML in C#

我正在开发一项服务,我在其中传递了一个 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">

.. 关于如何处理这种情况的任何建议?或者如何将这些属性放回它们在根元素中的位置?

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 移到顶部 - 如果它们停留在底部的位置,它会出于某种原因删除前缀)。

如果那不可能,您要么必须使用不同的序列化程序,要么编写一些自定义逻辑来 pre/post 处理您的 JSON 或 XML。