BoBasket4
BoBasket4

Reputation: 69

Error when trying to create an object from XML (C#)

I have an issue when trying to convert an xml string as an object, I keep getting "There is an error in XML document (1, 40)."

Here is my code :

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

The class :

[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; }
}

And an sample of the XML received :

<?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>

When I remove the the error is not on 1,40 but 1,2.

Thanks in advance !

Upvotes: 0

Views: 35

Answers (1)

BoBasket4
BoBasket4

Reputation: 69

I resolved the issue, I forgot to specify the namespace for the elements, for instance in Feed:

[XmlElement(ElementName = "entry", Namespace = "http://www.w3.org/2005/Atom")] 
public List<Entry> Entries { get; set; }

Upvotes: 1

Related Questions