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 !
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; }
Related
there is an xml of the form below, tell me how to deserialize it to get an object. using the YAXLib library
<catalog>
<categories>
<category id="1">category 1</category>
<category id="59349641">category 2</category>
<category id="303608809">category 3</category>
<category id="303614009">category 4</category>
<category id="303643009">category 5</category>
</categories>
</catalog>
how to get object from this xml ml with attributes
I tried this class but nothing is parsed
public class Сatalog
{
public List<Category> categories { get; set; }
}
public class Category
{
[YAXSerializeAs("id")]
public int Id { get; set; }
[YAXElementFor("category")]
public string category { get; set; }
[YAXSerializeAs("parentId")]
public int ParentId { get; set; }
}
tried like this
public class Сatalog
{
public List<string> categories { get; set; }
}
so we get only the text category
For anyone stumbling across a similar problem:
Here is the code to de/serialize:
[Test]
public void Xml_To_Object_YAXLib()
{
const string xml = #"<catalog>
<categories>
<category id=""1"">category 1</category>
<category id=""59349641"">category 2</category>
<category id=""303608809"">category 3</category>
<category id=""303614009"">category 4</category>
<category id=""303643009"">category 5</category>
</categories>
</catalog>";
var serializer = new YAXSerializer(typeof(Catalog),new SerializerOptions
{
ExceptionHandlingPolicies = YAXExceptionHandlingPolicies.ThrowWarningsAndErrors,
ExceptionBehavior = YAXExceptionTypes.Error,
SerializationOptions = YAXSerializationOptions.DontSerializeNullObjects
});
var deserialized = (Catalog) serializer.Deserialize(xml);
var serialized = serializer.Serialize(deserialized);
Assert.That(serialized, Is.EqualTo(xml));
Assert.That(deserialized.Categories.Count, Is.EqualTo(5));
Assert.That(deserialized.Categories[4].Id, Is.EqualTo(303643009));
}
And this is how to decorate classes with attributes to instruct the YAXSerializer:
[YAXSerializeAs("catalog")]
public class Catalog
{
[YAXSerializeAs("categories")]
public List<Category> Categories { get; set; }
}
and
[YAXSerializeAs("category")]
public class Category
{
[YAXSerializeAs("id")]
[YAXAttributeForClass]
public int Id { get; set; }
[YAXValueForClass]
public string CategoryName { get; set; }
/// <summary>
/// Assuming, this should also become an attribute to Category
/// (although DevOp's XML didn't contain it)
/// </summary>
[YAXSerializeAs("parentId")]
[YAXAttributeForClass]
[YAXErrorIfMissed(YAXExceptionTypes.Ignore)]
public int? ParentId { get; set; }
}
I am having issues creating the schema below...
<DocumentProperties>
<Document>
<Properties>
<propertyName>CNumber</propertyName>
<propertyValue>00645007803</propertyValue>
</Properties>
<targetFolder>\12345678\00645007803\</targetFolder>
</Document>
<Document>
<Properties>
<propertyName>CNumber</propertyName>
<propertyValue>00645007804</propertyValue>
</Properties>
<targetFolder>\12345678\00645007804\</targetFolder>
</Document>
</DocumentProperties>
I created the following classes to do this
public class DocumentProperties
{
public DocumentProperties()
{
Document = new List<Document>();
}
public List<Document> Document { get; set; }
}
public class Document
{
public Document()
{
Properties = new List<Properties>();
}
public List<Properties> Properties { get; set; }
public string targetFolder { get; set; }
}
public class Properties
{
public string propertyName { get; set; }
public string propertyValue { get; set; }
}
public class RetrieveMultipleDocumentsRequest
{
public SystemProperty SystemProperty { get; set; }
public RequestProperty RequestProperty { get; set; }
public DocumentProperties DocumentProperties { get; set; }
}
The output I am getting is "Document" and "Properties" twice as a parent child of each other. How do I resolve this?
Output from my classes
<DocumentProperties>
<Document>
<Document>
<Properties>
<Properties>
<propertyName>DizzzyGelespe</propertyName>
<propertyValue>8E077A60</propertyValue>
</Properties>
<Properties />
</Properties>
<targetFolder>C:\BXml\TargetFolder\</targetFolder>
</Document>
</Document>
</DocumentProperties>
Code that is generating the output:
public string Serialize(RetrieveMultipleDocumentsRequest details)
{
XmlSerializer serializer = new XmlSerializer(typeof(RetrieveMultipleDocumentsRequest));
using(StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, details);
return textWriter.ToString();
}
}
You will need to annotate your object model as shown below in order to change the default serialization behavior. This application of the XmlElement attribute will prevent emiting out the parent tag based upon the encountered property and instead only emit out the containing data.
public class DocumentProperties
{
public DocumentProperties()
{
Document = new List<Document>();
}
[XmlElement("Document")]
public List<Document> Document { get; set; }
}
public class Document
{
public Document()
{
Properties = new List<Properties>();
}
[XmlElement("Properties")]
public List<Properties> Properties { get; set; }
public string targetFolder { get; set; }
}
Apparently your naming convention confused the XML serializer a bit. Just explicitly decorate the elements as below and it should work fine:
public class DocumentProperties
{
public DocumentProperties()
{
Document = new List<Document>();
}
[XmlElement("Document")]
public List<Document> Document { get; set; }
}
public class Document
{
public Document()
{
Properties = new List<Properties>();
}
[XmlElement("Properties")]
public List<Properties> Properties { get; set; }
public string targetFolder { get; set; }
}
Your problem here is with naming.
A list of documents should be called "documents" not "document", same goes for properties.
If you make these naming changes then you will see that your XML output is correct and makes sense.
I need some help with XmlSerializer. I have to following xml fragment:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>
<id>tag:blogger.com,1999:blog-4233645339430781865.archive</id>
<updated>2012-10-22T07:00:02.139+03:00</updated>
<title type='text'>Code !t</title>
<link rel='alternate' type='text/html' href='http://www.etabakov.com/'/>
<author>
<name>Емил Табаков</name>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-TbRwL19G85U/AAAAAAAAAAI/AAAAAAAAFxg/NRV6ZYqd9Wg/s512-c/photo.jpg'/>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<entry>
<id>tag:blogger.com,1999:blog-4233645339430781865.post-513753811167440871</id>
<published>2012-10-12T11:22:35.759+03:00</published>
<updated>2012-10-12T11:22:35.759+03:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/blogger/2008/kind#comment'/>
<title type='text'>Great post indeed. I really like that you are prov...</title>
<content type='html'>Great post indeed. I really like that you are providing information on .NET for freshers , Being enrolled at http://www.wiziq.com/course/57-fresher-training-projects i found your information very helpful indeed. Thanks for it.</content>
<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4233645339430781865/8317071019326278340/comments/default/513753811167440871'/>
<author>
<name>sarabjeet</name>
<uri>http://www.blogger.com/profile/11223974173581186160</uri>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/>
</author>
<thr:in-reply-to href='http://www.etabakov.com/2012/06/net-guy-velocityconf-2012-day-1.html' ref='tag:blogger.com,1999:blog-4233645339430781865.post-8317071019326278340' source='http://www.blogger.com/feeds/4233645339430781865/posts/default/8317071019326278340' type='text/html'/>
<gd:extendedProperty name='blogger.itemClass' value='pid-899300522'/>
</entry>
</feed>
And I also have the following c# objects:
Feed.cs
[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom"), XmlType("feed")]
public class Feed
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement("entry")]
public List<Entry> Entry;
}
public class Entry
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("content")]
public string Content { get; set; }
[XmlElement("published")]
public DateTime Published { get; set; }
[XmlElement("updated")]
public DateTime Updated { get; set; }
[XmlElement("category")]
public List<Category> Categories;
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement(ElementName = "in-reply-to", Namespace = "thr", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
}
public class ReplyTo
{
[XmlAttribute("ref")]
public string Id { get; set; }
}
Everything works perfectly so far, except that ReplyTo property always stays null. I need to get the src attribute from the
I will be really happy if someone show me what I'm missing. Thanks!
The namespace you need is "http://purl.org/syndication/thread/1.0"
"thr" is just the alias - as declared by the xmlns:thr at the top.
So:
[XmlElement(ElementName = "in-reply-to", Namespace = "http://purl.org/syndication/thread/1.0", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
I need some help with XmlSerializer. I have to following xml fragment:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>
<id>tag:blogger.com,1999:blog-4233645339430781865.archive</id>
<updated>2012-10-22T07:00:02.139+03:00</updated>
<title type='text'>Code !t</title>
<link rel='alternate' type='text/html' href='http://www.etabakov.com/'/>
<author>
<name>Емил Табаков</name>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-TbRwL19G85U/AAAAAAAAAAI/AAAAAAAAFxg/NRV6ZYqd9Wg/s512-c/photo.jpg'/>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<entry>
<id>tag:blogger.com,1999:blog-4233645339430781865.post-513753811167440871</id>
<published>2012-10-12T11:22:35.759+03:00</published>
<updated>2012-10-12T11:22:35.759+03:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/blogger/2008/kind#comment'/>
<title type='text'>Great post indeed. I really like that you are prov...</title>
<content type='html'>Great post indeed. I really like that you are providing information on .NET for freshers , Being enrolled at http://www.wiziq.com/course/57-fresher-training-projects i found your information very helpful indeed. Thanks for it.</content>
<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4233645339430781865/8317071019326278340/comments/default/513753811167440871'/>
<author>
<name>sarabjeet</name>
<uri>http://www.blogger.com/profile/11223974173581186160</uri>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/>
</author>
<thr:in-reply-to href='http://www.etabakov.com/2012/06/net-guy-velocityconf-2012-day-1.html' ref='tag:blogger.com,1999:blog-4233645339430781865.post-8317071019326278340' source='http://www.blogger.com/feeds/4233645339430781865/posts/default/8317071019326278340' type='text/html'/>
<gd:extendedProperty name='blogger.itemClass' value='pid-899300522'/>
</entry>
</feed>
And I also have the following c# objects:
Feed.cs
[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom"), XmlType("feed")]
public class Feed
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement("entry")]
public List<Entry> Entry;
}
public class Entry
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("content")]
public string Content { get; set; }
[XmlElement("published")]
public DateTime Published { get; set; }
[XmlElement("updated")]
public DateTime Updated { get; set; }
[XmlElement("category")]
public List<Category> Categories;
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement(ElementName = "in-reply-to", Namespace = "thr", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
}
public class ReplyTo
{
[XmlAttribute("ref")]
public string Id { get; set; }
}
Everything works perfectly so far, except that ReplyTo property always stays null. I need to get the src attribute from the
I will be really happy if someone show me what I'm missing. Thanks!
The namespace you need is "http://purl.org/syndication/thread/1.0"
"thr" is just the alias - as declared by the xmlns:thr at the top.
So:
[XmlElement(ElementName = "in-reply-to", Namespace = "http://purl.org/syndication/thread/1.0", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
I need to serialize an Object to XML and back. The XML is fix and I can't change it.
I fail to generate this structure after bookingList.
How can I "group" these <booking> elements to appear as a LIST and keep <error> & <counter> before this List of <booking> elements.
See my example here:
Structure i need....
<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
<bookingList>
<error></error>
<counter>20</counter>
<booking>
<bookingID>1234567890</bookingID>
</booking>
<booking>
<bookingID>2345678901</bookingID>
</booking>
</bookingList>
</ownBookings>
</nicexml>
Structure i get with C# code below....
<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
<bookingList>
<booking>
<booking>
<bookingID>1234567890</bookingID>
</booking>
<booking>
<bookingID>2345678901</bookingID>
</booking>
<booking>
<error></error>
<counter>20</counter>
</bookingList>
</ownBookings>
</nicexml>
C# Code:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace xml_objects_serials
{
public class bookings
{
public class nicexml
{
public string key_id
{ get; set; }
public string surname
{ get; set; }
public string name
{ get; set; }
public int station_id
{ get; set; }
public ownBookings ownBookings
{ get; set; }
}
public class ownBookings
{
public bookingList bookingList
{ get; set; }
}
public class bookingList {
public string error
{ get; set; }
public int counter
{ get; set; }
public List<booking> booking= new List<booking>();
}
public class booking
{
public int bookingID
{ get; set; }
}
}
Try decorating the properties of the bookingListclass with the XmlElementAttribute, in order to control how the objects of that class are going to be serialized to XML.
Here's an example:
public class bookingList
{
[XmlElement(Order = 1)]
public string error { get; set; }
[XmlElement(Order = 2)]
public int counter { get; set; }
[XmlElement(ElementName = "booking", Order = 3)]
public List<booking> bookings = new List<booking>();
}
public class booking
{
public int id { get; set; }
}
In my test I obtained this output:
<?xml version="1.0" ?>
<bookingList>
<error>sample</error>
<counter>0</counter>
<booking>
<id>1</id>
</booking>
<booking>
<id>2</id>
</booking>
<booking>
<id>3</id>
</booking>
</bookingList>
Related resources:
Controlling XML Serialization Using Attributes
I was facing with this problem and i solved it... Well it is very interesting, and this is a bug in .net maybe.
the problem is here:
public List<booking> booking= new List<booking>();
You should use:
public List<booking> booking { get; set; }
And you will get the defined order .... but why? who knows... :)