在C#中使用JSON.net将人口普查Geocoder JSON输出转换为Xml数据集 [英] Census Geocoder JSON output convert to Xml dataset using JSON.net in C#
问题描述
我正在Visual Studio 2012中创建一个.Net应用程序,该应用程序查询SQL dB中的地址表,并使用人口普查地理编码API返回每个地址的特定MSA.我已经有了用于dB查询的代码,但是在将Census API的Json输出转换为Xml数据集时遇到了麻烦.我正在使用Json.net序列化json输出,然后反序列化为.net以便加载到XmlDocument中.不幸的是,我不断收到XmlException错误:
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 have existing code for the dB query, but I am having trouble with converting the Json output of the Census API to an Xml dataset. I am using Json.net to serialize the json output and then deserialize to .net in order to load into an XmlDocument. Unfortunately, I keep getting an XmlException error:
根级别的数据无效.第1行,位置1
Data at the root level is invalid. Line 1, position 1
详细信息:
未处理System.Xml.XmlException HResult = -2146232000
Message =根级别的数据无效.第1行,位置1.
Source = System.Xml LineNumber = 1 LinePosition = 1 SourceUri ="
堆栈跟踪: 在System.Xml.XmlTextReaderImpl.Throw(Exception e) 在System.Xml.XmlTextReaderImpl.Throw(String res,String arg) 在System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() 在System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在System.Xml.XmlTextReaderImpl.Read() 在System.Xml.XmlLoader.Load处(XmlDocument doc,XmlReader阅读器,布尔值saveWhitespace) 在System.Xml.XmlDocument.Load(XmlReader reader) 在System.Xml.XmlDocument.LoadXml(String xml) 在c:\ Users \ jdsmith \ Documents \ Visual Studio中的ConsoleApplication1.Program.Main(String [] args)中 2012 \ Projects \ C#\ MSA_Application_v2 \ MSA_Application_v2 \ Model \ Program.cs:line 54 在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []参数) 在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)中 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 reserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回调,对象状态,布尔值 reserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart()处InnerException:
System.Xml.XmlException was unhandled HResult=-2146232000
Message=Data at the root level is invalid. Line 1, position 1.
Source=System.Xml LineNumber=1 LinePosition=1 SourceUri=""
StackTrace: at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.LoadXml(String xml) at ConsoleApplication1.Program.Main(String[] args) in c:\Users\jdsmith\Documents\Visual Studio 2012\Projects\C#\MSA_Application_v2\MSA_Application_v2\Model\Program.cs:line 54 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
我相信Json或Xml都需要进一步格式化,但是我不知道如何.另外,我敢肯定我对自己来说太难了……如果有更好的方法,我会全神贯注.
I believe either the Json or the Xml need to be formatted further, but I don't know how. Also, I'm sure I am making this too difficult on myself...if there is a better way, I am all ears.
这是我用来测试的示例地理查找:
Here is the sample geolookup I am using to test:
首先,您要将JSON字符串传递给 First of all, you are passing a JSON string to 但是,您的某些JSON属性包含在中无法使用的字符XML名称,在特定的空格中: However, some of your JSON properties contain characters that are invalid for use in XML names, in specific whitespace:
这似乎导致 It seems that this causes 这篇关于在C#中使用JSON.net将人口普查Geocoder JSON输出转换为Xml数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!推荐答案
geoXMLDoc.LoadXml()
.那是行不通的.您要做的是通过 JsonConvert.DeserializeXmlNode
将JSON转换为XmlDocument
. geoXMLDoc.LoadXml()
. That's not going to work. What you want to do is to convert the JSON to an XmlDocument
via JsonConvert.DeserializeXmlNode
.
{"Census Blocks":[{"BLKGRP":"1",
DeserializeXmlNode
引发异常.因此,您需要重命名名称: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());