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

前端 未结 2 1011
夕颜
夕颜 2020-12-21 23:27

I am creating a .Net app in Visual Studio 2012 that queries an address table in my SQL dB and uses the Census Geocoding API to return the specific MSA for each address. I ha

2条回答
  •  春和景丽
    2020-12-22 00:02

    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()
                                 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());
    

提交回复
热议问题