C# 从XML转换后使用JSON.Net查询JSON时遇到问题

C# 从XML转换后使用JSON.Net查询JSON时遇到问题,c#,json.net,json-query,C#,Json.net,Json Query,我有一些XML,通过调用此方法将其转换为JSON: public static string SerializeAsJson(string xml) { if (string.IsNullOrEmpty(xml)) throw new ArgumentNullException(nameof(xml)); if (xml.StartsWith("{") && xml.EndsWith("}")) return xml; var doc = new Xml

我有一些XML,通过调用此方法将其转换为JSON:

public static string SerializeAsJson(string xml)
{
    if (string.IsNullOrEmpty(xml)) throw new ArgumentNullException(nameof(xml));
    if (xml.StartsWith("{") && xml.EndsWith("}")) return xml;

    var doc = new XmlDocument();
    doc.LoadXml(xml);

    var json = JsonConvert.SerializeXmlNode(doc);
    return json;
}
其中返回以下内容:

{
"?xml":{
    "@version":"1.0",
    "@encoding":"UTF-8"
},
"product":{
    "productname":{
        "#cdata-section":"Artisan Cook Processor - Onyx Black, 4.5 litres, Keep-warm function, Dishwasher-safe pot, Timer"
    },
    "productshortname":{
        "#cdata-section":"Artisan Cook Processor - Onyx Black"
    },
    "productshortdesc":{
        "#cdata-section":"Rethink the way you cook with the KitchenAid Artisan Cook Processor in stylish onyx black. The ultimate helping hand Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions. Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together. Simplicity, control and precision Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the Artisan Cook Processor . The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time. Uniquely stylish The 4.5 litre KitchenAid Artisan comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you don't need to spend hours manually maintaining it. Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring. Included extras Along with your KitchenAid Artisan Cook Processor , enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid Artisan Processor . If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid Artisan Cook Processor."
    },
    "producthtmldesc":{
        "#cdata-section":"<p>Rethink the way you cook with the KitchenAid <strong>Artisan Cook Processor</strong> in stylish onyx black.<br /> <br /> <strong>The ultimate helping hand</strong><br /> <br /> Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions.<br /> <br /> Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together.<br /> <br /> <strong>Simplicity, control and precision</strong><br /> <br /> Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the <strong>Artisan Cook Processor</strong>. The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time.<br /> <br /> <strong>Uniquely stylish</strong><br /> <br /> The 4.5 litre KitchenAid <strong>Artisan</strong> comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you dont need to spend hours manually maintaining it.<br /> <br /> Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring.<br /> <br /> <strong>Included extras</strong><br /> <br /> Along with your KitchenAid <strong>Artisan Cook Processor</strong>, enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid <strong>Artisan Processor</strong>.<br /> <br /> If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid <strong>Artisan Cook Processor. </strong></p>"
    },
    "productlongdesc":{
        "#cdata-section":"Rethink the way you cook with the KitchenAid Artisan Cook Processor in stylish onyx black. The ultimate helping hand Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions. Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together. Simplicity, control and precision Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the Artisan Cook Processor . The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time. Uniquely stylish The 4.5 litre KitchenAid Artisan comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you don't need to spend hours manually maintaining it. Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring. Included extras Along with your KitchenAid Artisan Cook Processor , enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid Artisan Processor . If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid Artisan Cook Processor."
    },
    "productsecondaryname":{
        "#cdata-section":"4.5 litres, Keep-warm function, Dishwasher-safe pot, Timer"
    },
    "brand":{
        "#cdata-section":"KITCHENAID"
    },
    "infostech":null,
    "refexternal":{
        "#cdata-section":"144626"
    },
    "infotech":{
        "#cdata-section":"[{\\"         Title\\":\\"OVERVIEW\\",
        \\"Priority\\":1,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Type\\",
            \\"Value\\":\\"Multicooker\\",
            \\"Priority\\":0
            },
            {
            \\"Title\\":\\"Power\\",
            \\"Value\\":\\"1500 W\\",
            \\"Priority\\":2
            },
            {
            \\"Title\\":\\"Capacity\\",
            \\"Value\\":\\"4.5 litres\\",
            \\"Priority\\":1
            }
        ]
    },
    {
        \\"Title\\":\\"FUNCTIONS\\",
        \\"Priority\\":2,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Number of programmes\\",
            \\"Value\\":\\"25\\",
            \\"Priority\\":4
            },
            {
            \\"Title\\":\\"Timer\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":8
            },
            {
            \\"Title\\":\\"Number of heat settings\\",
            \\"Value\\":\\"Up to 140�C\\",
            \\"Priority\\":7
            },
            {
            \\"Title\\":\\"Keep warm function\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":6
            },
            {
            \\"Title\\":\\"Programs\\",
            \\"Value\\":\\"- Boil- Fry- Stew- Steam- Puree- Dough\\",
            \\"Priority\\":5
            }
        ]
    },
    {
        \\"Title\\":\\"FEATURES\\",
        \\"Priority\\":3,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Digital display\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":10
            },
            {
            \\"Title\\":\\"Automatic shut-off\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":18
            },
            {
            \\"Title\\":\\"Dishwasher safe parts\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":17
            },
            {
            \\"Title\\":\\"Microwave safe pot\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":16
            },
            {
            \\"Title\\":\\"Oven safe pot\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":15
            },
            {
            \\"Title\\":\\"Number of tiers\\",
            \\"Value\\":\\"3\\",
            \\"Priority\\":14
            },
            {
            \\"Title\\":\\"Sound signal\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":12
            },
            {
            \\"Title\\":\\"Power indicator\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":11
            }
        ]
    },
    {
        \\"Title\\":\\"GENERAL\\",
        \\"Priority\\":4,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Colour\\",
            \\"Value\\":\\"Black\\",
            \\"Priority\\":20
            },
            {
            \\"Title\\":\\"Manufacturer's guarantee\\",
            \\"Value\\":\\"3 years\\",
            \\"Priority\\":26
            },
            {
            \\"Title\\":\\"Weight\\",
            \\"Value\\":\\"10.14 kg\\",
            \\"Priority\\":25
            },
            {
            \\"Title\\":\\"Dimensions \\",
            \\"Value\\":\\"411 x 314 x 342 mm (H x W x D)\\",
            \\"Priority\\":24
            },
            {
            \\"Title\\":\\"Power cord length\\",
            \\"Value\\":\\"1 m\\",
            \\"Priority\\":23
            },
            {
            \\"Title\\":\\"Power cord storage\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":22
            },
            {
            \\"Title\\":\\"Box contents\\",
            \\"Value\\":\\"- StirAssist- Egg whip- Dough blade- Multiblade- Upper and lower steam baskets with lid- Inner basket- Mini bowl and mini multiblade- Measuring cup- Free cookbook\\",
            \\"Priority\\":21
            }
        ]
    }
]   "},"   id":{
    "#cdata-section":"10136069"
},
"images":{
    "urlimage1":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069.jpg",
    "urlimage2":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069.jpg",
    "urlimage3":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069.jpg",
    "urlimage4":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069.jpg",
    "urlimage5":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069.jpg",
    "urlimage6":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/u_10136069.jpg"
},
"images_alternate":{
    "urlimage1":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_003.jpg",
    "urlimage2":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_003.jpg",
    "urlimage3":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_003.jpg",
    "urlimage4":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_003.jpg",
    "urlimage5":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_003.jpg",
    "urlimage6":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_001.jpg",
    "urlimage7":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_001.jpg",
    "urlimage8":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_001.jpg",
    "urlimage9":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_001.jpg",
    "urlimage10":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_001.jpg",
    "urlimage11":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_006.jpg",
    "urlimage12":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_006.jpg",
    "urlimage13":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_006.jpg",
    "urlimage14":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_006.jpg",
    "urlimage15":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_006.jpg",
    "urlimage16":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_005.jpg",
    "urlimage17":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_005.jpg",
    "urlimage18":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_005.jpg",
    "urlimage19":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_005.jpg",
    "urlimage20":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_005.jpg",
    "urlimage21":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_002.jpg",
    "urlimage22":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_002.jpg",
    "urlimage23":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_002.jpg",
    "urlimage24":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_002.jpg",
    "urlimage25":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_002.jpg",
    "urlimage26":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_004.jpg",
    "urlimage27":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_004.jpg",
    "urlimage28":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_004.jpg",
    "urlimage29":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_004.jpg",
    "urlimage30":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_004.jpg"
},
"universe":{
    "#cdata-section":"Household Appliances"
},
"universeid":"35",
"category":{
    "#cdata-section":"Small kitchen appliances"
},
"categoryid":"336",
"market":{
    "#cdata-section":"Small cooking appliances"
},
"marketid":"3155",
"segment":{
    "#cdata-section":"Multicookers"
},
"segmentid":"31992",
"MPN":{
    "#cdata-section":"000000859792415020"
},
"EAN":"5413184100599",
"color":null,
"weight":"1"
}
}
这就给了我:

{
    [
        {
            "Title":"OVERVIEW",
            "Priority":1,
            "Attributes":[
                {
                "Title":"Type",
                "Value":"Multicooker",
                "Priority":0
                },
                {
                "Title":"Power",
                "Value":"1500 W",
                "Priority":2
                },
                {
                "Title":"Capacity",
                "Value":"4.5 litres",
                "Priority":1
                }
            ]
        },
        {
            "Title":"FUNCTIONS",
            "Priority":2,
            "Attributes":[
                {
                "Title":"Number of programmes",
                "Value":"25",
                "Priority":4
                },
                {
                "Title":"Timer",
                "Value":"Yes",
                "Priority":8
                },
                {
                "Title":"Number of heat settings",
                "Value":"Up to 140�C",
                "Priority":7
                },
                {
                "Title":"Keep warm function",
                "Value":"Yes",
                "Priority":6
                },
                {
                "Title":"Programs",
                "Value":"- Boil- Fry- Stew- Steam- Puree- Dough",
                "Priority":5
                }
            ]
        },
        {
            "Title":"FEATURES",
            "Priority":3,
            "Attributes":[
                {
                "Title":"Digital display",
                "Value":"Yes",
                "Priority":10
                },
                {
                "Title":"Automatic shut-off",
                "Value":"Yes",
                "Priority":18
                },
                {
                "Title":"Dishwasher safe parts",
                "Value":"Yes",
                "Priority":17
                },
                {
                "Title":"Microwave safe pot",
                "Value":"No",
                "Priority":16
                },
                {
                "Title":"Oven safe pot",
                "Value":"No",
                "Priority":15
                },
                {
                "Title":"Number of tiers",
                "Value":"3",
                "Priority":14
                },
                {
                "Title":"Sound signal",
                "Value":"Yes",
                "Priority":12
                },
                {
                "Title":"Power indicator",
                "Value":"Yes",
                "Priority":11
                }
            ]
        },
        {
            "Title":"GENERAL",
            "Priority":4,
            "Attributes":[
                {
                "Title":"Colour",
                "Value":"Black",
                "Priority":20
                },
                {
                "Title":"Manufacturer's guarantee",
                "Value":"3 years",
                "Priority":26
                },
                {
                "Title":"Weight",
                "Value":"10.14 kg",
                "Priority":25
                },
                {
                "Title":"Dimensions ",
                "Value":"411 x 314 x 342 mm (H x W x D)",
                "Priority":24
                },
                {
                "Title":"Power cord length",
                "Value":"1 m",
                "Priority":23
                },
                {
                "Title":"Power cord storage",
                "Value":"No",
                "Priority":22
                },
                {
                "Title":"Box contents",
                "Value":"- StirAssist- Egg whip- Dough blade- Multiblade- Upper and lower steam baskets with lid- Inner basket- Mini bowl and mini multiblade- Measuring cup- Free cookbook",
                "Priority":21
                }
            ]
        }
    ]
}
我想将其转换为IList,但无论我做什么尝试,都会得到一个错误。有人能帮我吗

另外,如果我看一下产品的价值,它显示了一个数组:


我认为问题在于
product.infotech.\cdata部分的值是一个JSON字符串;它还不是一个数组。您可以在调试器屏幕截图中看到这一点:
类型
列为
字符串
(而不是
数组
)。可能原始XML在该标记中包含JSON。当整个过程转换为JSON时,已经存在的JSON被转义,这就是为什么会出现所有额外的反斜杠。因此,要将值恢复到可用的形式,需要重新解析该部分:

var json = JObject.Parse(response);

// get the escaped JSON as a string
var productsJson = (string)json.SelectToken("product.infotech.#cdata-section");

// re-parse it to a JArray
var productsArray = JArray.Parse(productsJson);

// Now you can iterate over the array to access the JObjects within it
foreach (JObject item in productsArray)
{
    ...
}
var json = JObject.Parse(response);

// get the escaped JSON as a string
var productsJson = (string)json.SelectToken("product.infotech.#cdata-section");

// re-parse it to a JArray
var productsArray = JArray.Parse(productsJson);

// Now you can iterate over the array to access the JObjects within it
foreach (JObject item in productsArray)
{
    ...
}