我在尝试将xml字符串转换为对象时遇到问题,我一直在“XML文档中存在错误(1,40)。”
这是我的代码:
httpResponse = await httpClient.GetAsync(requestUri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
XmlSerializer serializer = new XmlSerializer(typeof(Feed));
StringReader rdr = new StringReader(httpResponseBody);
Feed resultingMessage = (Feed)serializer.Deserialize(rdr);
班级:
[XmlRoot("feed"), Serializable]
public class Feed
{
[XmlElement("title")]
public string title { get; set; }
[XmlElement("entry")]
public List<Entry> Entry { get; set; }
}
public class Entry
{
[XmlElement("content")]
public Content content { get; set; }
}
public class Content
{
[XmlElement("properties")]
public Properties properties { get; set; }
}
public class Properties
{
[XmlElement("EntityID")]
public int properties { get; set; }
[XmlElement("Latitude")]
public Double latitude { get; set; }
[XmlElement("Longitude")]
public Double longitude { get; set; }
[XmlElement("DisplayName")]
public Properties name { get; set; }
[XmlElement("__Distance")]
public Double distance { get; set; }
}
收到的XML示例:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text"></title>
<id>uuid:ce35d2a9-1966-4bd7-8730-80cff2a0ce58;id=13760</id>
<rights type="text">© 2017 Microsoft and its suppliers. This API and any results cannot be used or accessed without Microsoft's express written permission.</rights>
<updated>2017-06-28T19:27:04Z</updated>
<entry>
<id>https://spatial.virtualearth.net/REST/v1/data/c2ae584bbccc4916a0acf75d1e6947b4/NavteqEU/NavteqPOIs('800791175')</id>
<title type="text"></title>
<updated>2017-06-28T19:27:04Z</updated>
<content type="application/xml">
<m:properties>
<d:EntityID>800791175</d:EntityID>
<d:Latitude m:type="Edm.Double">47.386450</d:Latitude>
<d:Longitude m:type="Edm.Double">0.690600</d:Longitude>
<d:DisplayName>Au Chantecler</d:DisplayName>
<d:LanguageCode>FRE</d:LanguageCode>
<d:__Distance m:type="Edm.Double">0.0730920601952144</d:__Distance>
</m:properties>
</content>
</entry>
</feed>
当我删除 时,错误不在1,40而是1,2。
提前致谢!
答案 0 :(得分:1)
我解决了这个问题,我忘了为元素指定命名空间,例如Feed
:
[XmlElement(ElementName = "entry", Namespace = "http://www.w3.org/2005/Atom")]
public List<Entry> Entries { get; set; }