Census Geocoder JSON output convert to Xml dataset using JSON.net in C#

杀马特。学长 韩版系。学妹 提交于 2019-11-29 17:05:46

First of all, you are passing a JSON string to geoXMLDoc.LoadXml(). That's not going to work. What you want to do is to convert the JSON to an XmlDocument via JsonConvert.DeserializeXmlNode.

However, some of your JSON properties contain characters that are invalid for use in XML names, in specific whitespace:

{"Census Blocks":[{"BLKGRP":"1",

It seems that this causes DeserializeXmlNode to throw an exception. Thus you'll need to rename the names:

        var obj = JObject.Parse(geoString);
        foreach (var fix in (from property in obj.Descendants().OfType<JProperty>()
                             let newName = XmlConvert.EncodeLocalName(property.Name.Replace(" ", ""))
                             where newName != property.Name
                             select new { Old = property, New = new JProperty(newName, property.Value) })
                   .ToList())
        {
            fix.Old.Replace(fix.New);
        }

        var xmldoc = JsonConvert.DeserializeXmlNode(obj.ToString());

Need you to post what you are attempting to load into the XmlDocument. That is where you are hitting your problem. If you are trying to load the JSON you get from the web call it won't work, if you are (as I suspect) using JSON.Net to convert the JSON to Xml, the Xml is missing something that the XmlDocument wants. Could be the Xml declaration line, or your xml fragment may not include a root node. Without seeing the xml we have no way to tell specifically what is missing or malformed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!