使用带有根元素的有效Json的JsonReaderWriterFactory.CreateJsonReader会导致XmlException(“Root元素缺失”)的原因是什么?

时间:2014-09-29 11:14:47

标签: xml json httpresponse

我正在尝试使用.Net JsonReaderWriterFactory将JSON转换为XML。我正在使用的Json是有效的(由http://jsonformatter.curiousconcept.com/验证)并且当使用JsonConvert时,相同JSON的转换有效,但是当尝试用本机.Net功能替换Json.Net功能时(需要在哪里)我正在工作)我得到一个XmlException"缺少Root元素"。

我正在使用的测试JSON是:

{
        "customers"    :   [        
    {
        "id":               "b8ca3a6e-72c0-11e4-efc6-455d6c1876e5",
        "name":             "",
        "customer_code":    "WALKIN",
        "customer_group_id":         "b8ca3a6e-72c0-11e4-efc6-455d6c1840bb",
        "customer_group_name":       "All Customers",
        "updated_at":               "2014-09-26 09:13:47"
    }
,       
    {
        "id":               "b8ca3a6e-72a9-11e4-efc6-455deebc001e",
        "name":             "Caroline Chambers",
        "customer_code":    "Caroline-79T7",
        "customer_group_id":         "b8ca3a6e-72c0-11e4-efc6-455d6c1840bb",
        "customer_group_name":       "All Customers",
        "first_name":             "Caroline",
        "last_name":              "Chambers",
        "email":                  "caroline.chambers@hotmail.com",
        "postal_country_id" :     "GB",        
        "updated_at":               "2014-09-26 09:17:26",
        "date_of_birth":            "1980-02-20",
        "sex":                      "F"
    }
,   
    {
        "id":               "b8ca3a6e-72a9-11e4-efc6-455e1b7bcc0f",
        "name":             "Andrew Adamson",
        "customer_code":    "Andrew-85SW",
        "customer_group_id":         "b8ca3a6e-72c0-11e4-efc6-455d6c1840bb",
        "customer_group_name":       "All Customers",
        "first_name":             "Andrew",
        "last_name":              "Adamson",
        "email":                  "andrew.adamson@hotmail.com",
        "postal_country_id" :     "GB",
        "updated_at":               "2014-09-26 09:18:41",
        "date_of_birth":            "1980-06-01",
        "sex":                      "M"
    }
    ]
}

我的代码是:

XmlDocument document = new XmlDocument();

using (Stream dataStream = response.GetResponseStream())
{
    using (XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(dataStream, XmlDictionaryReaderQuotas.Max))
    {
        xmlReader.Read();
        document.LoadXml(xmlReader.ReadOuterXml());
    }
}

与Json.Net使用相同的JSON响应的等效工作没有错误:

XmlDocument document = new XmlDocument();

using (Stream dataStream = response.GetResponseStream())
{
    using (StreamReader reader = new StreamReader(dataStream))
    {
        string responseFromServer = reader.ReadToEnd();
        document = JsonConvert.DeserializeXmlNode(responseFromServer, "Root");
        reader.Close();
    }
}

在调用CreateJsonReader创建有效的根元素之前,我需要对响应流做什么?

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,JsonReaderWriterFactory代码 正在工作。

我有一行代码:

StreamReader reader = new StreamReader(dataStream))
string responseFromServer = reader.ReadToEnd();

在调用JsonReaderWriterFactory之前,为了调试目的而输出我的Json(为简单起见,省略了一行代码)。这意味着当dataStream用于转换时,它是空的,已经被读取结束。如果没有它,它现在可以按预期工作。