我有一个很大的JSON字符串,我试图转换为xml。我正在使用JSON.net来执行此操作,并且我有以下代码行。
XmlDocument doc = JsonConvert.DeserializeXmlNode(jsonstring);
当我执行此行时,它会抛出异常
“'字符,十六进制值0x20,不能包含在a中 名“。
从我能找到的这个错误是由于xml节点名称中的空格。我检查了JSON,并且没有名称中有空格的字段名称。某些数据值具有空格。例如“addressFullName”:“Mike Spark”,但这应该没问题,因为它在节点内。
json = json.Replace(" ", "");
上面的代码行解决了错误,但它删除了任何字符串之间的空格,例如需要有空格的地址信息。
更新:原来有1个节点有一个空格,1个是数字。代码来自Web服务,因此我无法控制格式。我需要找到一种方法来删除字段名称中的空格或设置一些规则以在检测到错误时强制有效。
堆栈跟踪
at System.Xml.XmlDocument.CheckName(String name)
at System.Xml.XmlElement..ctor(XmlName name, Boolean empty, XmlDocument doc)
at System.Xml.XmlDocument.CreateElement(String prefix, String localName, String namespaceURI)
at System.Xml.XmlDocument.CreateElement(String name)
at Newtonsoft.Json.Converters.XmlDocumentWrapper.CreateElement(String elementName)
at Newtonsoft.Json.Converters.XmlNodeConverter.CreateElement(String elementName, IXmlDocument document, String elementPrefix, XmlNamespaceManager manager)
at Newtonsoft.Json.Converters.XmlNodeConverter.ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, String propertyName, XmlNamespaceManager manager)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeValue(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, String propertyName, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, String propertyName, XmlNamespaceManager manager)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeValue(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, String propertyName, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, String propertyName, XmlNamespaceManager manager)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeValue(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, String propertyName, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, String propertyName, XmlNamespaceManager manager)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeValue(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, String propertyName, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, String propertyName, XmlNamespaceManager manager)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeValue(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, String propertyName, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode)
at Newtonsoft.Json.Converters.XmlNodeConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonConverter[] converters)
at Newtonsoft.Json.JsonConvert.DeserializeXmlNode(String value, String deserializeRootElementName, Boolean writeArrayAttribute)
at Newtonsoft.Json.JsonConvert.DeserializeXmlNode(String value, String deserializeRootElementName)
at Newtonsoft.Json.JsonConvert.DeserializeXmlNode(String value)