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; }
}
Related
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; }
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´m trying to deserialize this XML to objects in C# .NET 4.5:
<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
<item id="28" parentID="19" restricted="1">
<dc:creator>Alicia Keys</dc:creator>
<dc:date>2003-01-01</dc:date>
<dc:title>Gangsta Lovin' (feat. Alicia Keys)</dc:title>
</item>
</DIDL-Lite>
Code:
I´m not getting any "item" Lists. The object isn't deserialized.
MemoryStream reader = new MemmoryStream(System.Text.Encoding.Unicode.GetBytes(Result));
var ser = new XmlSerializer(typeof(DIDLLite));
DIDLLite device = (DIDLLite)ser.Deserialize(reader);
Class DIDLLite:
[XmlRoot("DIDL-Lite", Namespace = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/")]
public class DIDLLite {
DIDLLite() {
this.serviceItem = new List<ContainerItem>();
}
[System.Xml.Serialization.XmlArrayItem("item", typeof(ServiceListTypeService), IsNullable = false)]
List<ContainerItem> serviceItem = new List<ContainerItem>();
}
Class ContainerItem:
public class ContainerItem
{
[System.Xml.Serialization.XmlAttribute("id")]
public string id { get; set; }
[System.Xml.Serialization.XmlAttribute("parentID")]
public string parentID { get; set; }
[System.Xml.Serialization.XmlAttribute("restricted")]
public string restricted { get; set; }
[System.Xml.Serialization.XmlAttribute("searchable")]
public string searchable { get; set; }
public string title { get; set; }
}
You have several issues:
you define an XmlArrayItem attribute - but really, in your XML, you don't have any list of items. If you want to use an Xml array construct, you'd need to have something like this for your XML:
<DIDL-Lite .....>
<Items>
<item id="28" parentID="19" restricted="1">
......
</item>
<item id="29" parentID="19" restricted="1">
......
</item>
</Items>
</DIDL-Lite>
So you would need to have an <Items>...</Items> wrapper around your <item> elements.
You have this declaration:
[XmlArrayItem("item", typeof(ServiceListTypeService), IsNullable = false)]
but where is the ServiceListtypeService defined? I don't see any trace of it....
I simplified your code a bit - and this works just fine:
[XmlRoot("DIDL-Lite", Namespace = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/")]
public class DIDLLite
{
[XmlElement("item")]
public ContainerItem Item { get; set; }
}
public class ContainerItem
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlAttribute("parentID")]
public string parentID { get; set; }
[XmlAttribute("restricted")]
public string restricted { get; set; }
[XmlAttribute("searchable")]
public string searchable { get; set; }
// you were missing these elements and their namespace
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
public string creator { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
public string date { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
public string title { get; set; }
}
And now, when I run your code to deserialize your XML, I do get the objects filled nicely.
I need to create an object that can be deserialised from this XML
<Item>
<Description>Timber(dry)</Description>
<Measure Type="VOLUME">
<Value>1.779</Value>
<Units>m3</Units>
</Measure>
<Measure Type="WEIGHT">
<Value>925.08</Value>
<Units>Kilogram</Units>
</Measure>
<Measure>
<Value>1</Value>
<Units>Units</Units>
</Measure>
</Item>
My Problem is the Measure, it needs to be a list of some sort but when i create a
list it serialises incorrectly
<Item>
<Description>Timber(dry)</Description>
<Measures> <--- Dont want this <Measures> tag
<Measure Type="VOLUME">
<Value>1.779</Value>
<Units>m3</Units>
</Measure>
<Measure Type="WEIGHT">
<Value>925.08</Value>
<Units>Kilogram</Units>
</Measure>
<Measure>
<Value>1</Value>
<Units>Units</Units>
</Measure>
</Measures> <---
</Item>
This is what i have so far
public class Item
{
public Item()
{
this.Measures = new List<Measure>();
}
public string Description { get; set; }
public List<Measure> Measures { get; set; }
}
public class Measure
{
public string Value { get; set; }
public string Units { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type { get; set; }
}
You need to decorate the Measures property with the [XmlElement] attribute, to indicate to the serializer that it needs to be serialized (and deserialized) as (bare) elements, instead of them being wrapped in another element.
public class StackOverflow_13188624
{
const string XML = #" <Item>
<Description>Timber(dry)</Description>
<Measure Type=""VOLUME"">
<Value>1.779</Value>
<Units>m3</Units>
</Measure>
<Measure Type=""WEIGHT"">
<Value>925.08</Value>
<Units>Kilogram</Units>
</Measure>
<Measure>
<Value>1</Value>
<Units>Units</Units>
</Measure>
</Item>";
public class Item
{
public Item()
{
this.Measures = new List<Measure>();
}
public string Description { get; set; }
[System.Xml.Serialization.XmlElement(ElementName = "Measure")]
public List<Measure> Measures { get; set; }
}
public class Measure
{
public string Value { get; set; }
public string Units { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type { get; set; }
}
public static void Test()
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
XmlSerializer xs = new XmlSerializer(typeof(Item));
Item item = (Item)xs.Deserialize(ms);
Console.WriteLine(item.Measures);
}
}
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; }