Deserialization XmlArray not populating - c#

I am trying to deserialize a XML response from an API using C#. I am not able to get the values of the ItemList element. I am able to get the other elements besides the ItemList. A sample XML is below and Classes below that, and finally the serialize function.
XML:
<Root xmlns="http://whatever">
<Child1>
<ChildElement1>16f09999</ChildElement1>
</Child1>
<ItemList>
<Element1>
...
</Element1>
<Item>
<ItemElem1>12345567</ItemElem1>
<ItemElem2>12356465784</ItemElem2>
...
<ItemAttributes>
...
</ItemAttributes>
</Item>
<Item>
...
</Item>
</ItemList>
</Root>
[Serializable()]
[System.Xml.Serialization.XmlRoot("Root")]
public class ResponseClass
{
[System.Xml.Serialization.XmlElement("Child1")]
public OperationRequest OperationRequest { get; set; }
[System.Xml.Serialization.XmlElement("ItemList")]
public ItemList ItemList { get; set; }
}
Class:
[Serializable()]
[System.Xml.Serialization.XmlRoot(ElementName = "ItemList")]
public class ItemList
{
[XmlArray("ItemList")]
[XmlArrayItem("Item", typeof(Item))]
public Item item { get; set; }
[System.Xml.Serialization.XmlElement("Element1")]
public int Element1 { get; set; }
}
Serializer:
XmlSerializer serializer = new XmlSerializer(typeof(ResponseClass));
StreamReader reader = new StreamReader(path);
response = (ResponseClass)serializer.Deserialize(reader);
reader.Close();

Please try the following. Since ItemList/Item is not a XmlArray, you need to define it as a simple XmlElement.
[Serializable()]
[System.Xml.Serialization.XmlRoot(ElementName = "Root", Namespace = "http://whatever")]
public class ResponseClass
{
[System.Xml.Serialization.XmlElement("Child1")]
public OperationRequest OperationRequest { get; set; }
[System.Xml.Serialization.XmlElement("ItemList")]
public ItemList ItemList { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot(ElementName = "ItemList", Namespace = "http://whatever")]
public class ItemList
{
[System.Xml.Serialization.XmlElement("Item")]
public Item[] Items { get; set; }
[System.Xml.Serialization.XmlElement("Element1")]
public int Element1 { get; set; }
}

Related

C# Parsing an XML and storing it into a Class

I need to parse an XML and store it into a class but it's been giving me a lot more headaches than I'm happy to admit.
Assume the XML looks like this:
<PARENT>
<ITEM>
<DATA1>
<A>12345</A>
<B>12345</B>
<C>12345</C>
</DATA1>
<DATA2>
<D>12345</D>
<E>12345</E>
<F>12345</F>
</DATA2>
<DATA3>
<ITEM>
<G/>
<H>111</H>
<I>223</I>
</ITEM>
<ITEM>
<G/>
<H>2342</H>
<I>25323</I>
</ITEM>
<ITEM>
<G>185</G>
<H>63611</H>
<I/>
</ITEM>
</DATA3>
</ITEM>
<ITEM>
<DATA1>
<A>23456</A>
<B>23456</B>
<C>23456</C>
</DATA1>
</ITEM>
</PARENT>
The first problem I'm facing is the fact that the name "ITEM" doesn't only appear as a childnode of "PARENT" but is also reused again under of "DATA3" so if I just search like this
XDocument doc = XDocument.Parse(tmp);
IEnumerable<XElement> tmp_list = doc.Descendants(XName.Get("ITEM"));
it will give me gives me five results instead of just two.
I've also already tried the approach of searching the path //PARENT/ITEM like this:
string file = #"C:\temp\test.xml";
var document = XDocument.Load(file);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("empty", "random:namespace:name");
IEnumerable<XElement> ele_list = document.XPathSelectElements("//PARENT//ITEM", namespaceManager);
but it doesn't work either since it also fetches the all the grandchild nodes...
Question 1) How can I correctly restrict the search to just the top level nodes?
Question 2) How would I then proceed and store the whole thing into a class in the most efficient way? I've tried copypasting the full XML response to websites like http://xmltocsharp.azurewebsites.net/ but all of them seem to be struggling with the duplicate name "ITEM" and basically create one class that has merged ALL of the different "ITEM" child nodes within. Is it possible to fill a List of items in a simple and efficient way? I was hoping to have essentially something along the lines of:
List<DataItem> grd_list = doc.Descendants(XName.Get("ITEM"))
.Select(f => new DataItem()
{
DATA1 = f.Element(XName.Get("DATA1")).Document.ToString(),
DATA2 = f.Element(XName.Get("DATA2")).Document.ToString(),
//etc.....
}).ToList();
but I'm aware that the above LINQ would have to be more complex than this since DATA1 etc also needs to have child nodes.
you could use serializer XML (its just an example following your xml)
you could avoid the problem of item by defining all cases in the definition of the class ITEM. So the items not presents (G,H,I or DATA1,DATA2.. will be null).
using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(PARENT));
using (StringReader reader = new StringReader(xml))
{
var test = (PARENT)serializer.Deserialize(reader);
}
:
:
[XmlRoot(ElementName = "DATA1")]
public class DATA1
{
[XmlElement(ElementName = "A")]
public string A { get; set; }
[XmlElement(ElementName = "B")]
public string B { get; set; }
[XmlElement(ElementName = "C")]
public string C { get; set; }
}
[XmlRoot(ElementName = "DATA2")]
public class DATA2
{
[XmlElement(ElementName = "D")]
public string D { get; set; }
[XmlElement(ElementName = "E")]
public string E { get; set; }
[XmlElement(ElementName = "F")]
public string F { get; set; }
}
[XmlRoot(ElementName = "ITEM")]
public class ITEM
{
[XmlElement(ElementName = "G")]
public string G { get; set; }
[XmlElement(ElementName = "H")]
public string H { get; set; }
[XmlElement(ElementName = "I")]
public string I { get; set; }
[XmlElement(ElementName = "DATA1")]
public DATA1 DATA1 { get; set; }
[XmlElement(ElementName = "DATA2")]
public DATA2 DATA2 { get; set; }
[XmlElement(ElementName = "DATA3")]
public DATA3 DATA3 { get; set; }
}
[XmlRoot(ElementName = "DATA3")]
public class DATA3
{
[XmlElement(ElementName = "ITEM")]
public List<ITEM> ITEM { get; set; }
}
[XmlRoot(ElementName = "PARENT")]
public class PARENT
{
[XmlElement(ElementName = "ITEM")]
public List<ITEM> ITEM { get; set; }
}

C# Deserializing a XML to an object List

I have this XML structure:
<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/" action="EXPORT">
<result>
<rows>
<row>
<column name="Name1">Value1</column>
<column name="Name2">Value2</column>
</row>
<row>
<column name="Name1">Value1</column>
<column name="Name2">Value2</column>
</row>
</rows>
</result>
</response>
I'm trying to deserialize the XML into a list object like so:
List<ModelXML> model;
using (TextReader reader = new StringReader(xml_str))
{
System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(List<ModelXML>),
new XmlRootAttribute("rows"));
model= (List<ModelXML>)deserializer.Deserialize(reader);
}
My parameters in the ModelXML Class:
[XmlElement("Name1")]
public string Name1{ set; get; }
[XmlElement("Name2")]
public string Name2{ set; get; }
And finally I'm getting this error:
The '=' character, hexadecimal value 0x3D, cannot be included in a
name. Line 1, position 13.
What I'm doing wrong? thanks.
Here how you can solve your issue
First you have to change your model
[XmlRoot(ElementName = "column")]
public class Column
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "row")]
public class Row
{
[XmlElement(ElementName = "column")]
public List<Column> Column { get; set; }
}
[XmlRoot(ElementName = "rows")]
public class Rows
{
[XmlElement(ElementName = "row")]
public List<Row> Row { get; set; }
}
[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "rows")]
public Rows Rows { get; set; }
}
[XmlRoot(ElementName = "response")]
public class Response
{
[XmlElement(ElementName = "result")]
public Result Result { get; set; }
[XmlAttribute(AttributeName = "uri")]
public string Uri { get; set; }
[XmlAttribute(AttributeName = "action")]
public string Action { get; set; }
}
Then
use the deserialization code like the following
//here I'm trying to load the file from the disk but you can do the same by passing a string
Response model;
var xml = File.ReadAllText("file.xml");
using (TextReader reader = new StringReader(xml))
{
System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(Response));
model = (Response)deserializer.Deserialize(reader);
}
More you can access your rows like this
var rows = model.Result.Rows;
//hope this can help
When I run your code I get the following exception.
System.InvalidOperationException: 'There is an error in XML document (2, 2).'
InnerException
InvalidOperationException: < response xmlns='' > was not expected.
This is because you must define the outer most element as the root element and not one deeper in the structure like your tried to do. One way to solve this is by defining all nessesary model classes like #BRAHIM Kamel did.
Ïf you don't want to have so many model classes and you don't care to much about performance you could also do this.
[XmlRoot("row")]
public class ModelXML
{
public class Column
{
[XmlAttribute("name")]
public string Name { set; get; }
[XmlText]
public string Value { set; get; }
}
[XmlElement("column")]
public List<Column> Columns { get; set; }
}
IEnumerable<T> Deserialize<T>(IEnumerable<XElement> elements)
{
foreach (var element in elements)
{
using (var reader = XDocument.Parse(element.ToString()).CreateReader())
{
XmlSerializer deserializer = new XmlSerializer(typeof(T));
yield return (T)deserializer.Deserialize(reader);
}
}
}
var document = XDocument.Parse(xml_str);
var collection = Deserialize<ModelXML>(
document.XPathSelectElements("response/result/rows/row"));

Unexpected results when serializing objects to XML

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.

deserialize xml into inherited classes from base class

I have the following xml structure:
<Root1>
<name>Name1</name>
<company>Comp1</company>
<url>site.com</url>
<elements>
<element id="12" type="1">
<url>site1.com</url>
<price>15000</price>
...
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
</element>
<element id="13" type="2">
<url>site2.com</url>
<price>100</price>
...
<language>lg</language>
<binding>123</binding>
</element>
</elements>
</Root1>
I need to deserialize this xml into an object. You can see the element contains some equals field: url and price.
I would like to move these fields into a parent class and then inherit this class from other classes.
I created the class Root1:
namespace app1
{
[Serializable]
public class Root1
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("elements")]
public List<Element> ElementList { get; set; }
}
}
and then I created base class for Element:
[Serializable]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
}
and then I inherited this class from other classes:
[Serializable]
public class Element1 : Element
{
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
}
[Serializable]
public class Element2 : Element
{
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
When I deserialize this xml to object Root1 I get the object - it is ok.
But the List of Elements contains only Element objects not Element1 and Element2 objects.
How I do deserialize this xml so list of Elements contains Element1 and Element2 objects?
#netwer It is not working for you because the code suggested above generates the xml (below) that does not match with the one you use for deserialization (see how it specifies derived types in for element).
<?xml version="1.0" encoding="utf-8"?>
<Root1>
<name>Name1</name>
<company>Comp1</company>
<url>site.com</url>
<elements>
<element d3p1:type="Element1" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site1.com</url>
<price>15000</price>
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
</element>
<element d3p1:type="Element2" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site2.com</url>
<price>100</price>
<language>lg</language>
<binding>123</binding>
</element>
</elements>
</Root1>
So you will either have to match this format with source xml (change code or API that returns this xml data) or take another approach. Even if you manage to do with former one you have to find a way to access Element1 or Element2 specific properties.
newRoot1.Elements.ElementList[i] will always let you access only price and url since your list is of Element type. Although run time type of ElementList[i] will be Element1 or Element2, how will you detect that?
Here I suggest alternative approach. Irrespective of whether your application (client) generates this xml or the server which returns it on hitting API, you should be able to gather information on all the fields applicable to an 'element' object. If it is your code you know it already, if it is an API there must be a document for it. This way you only have to create one 'Element' (no derived classes) and put proper checks (mostly string.IsNullOrEmpty()) before accessing Element class property values in your code. Only the properties that are present in your xml 'element' element will be considered rest will be set to NULL for that instance.
[Serializable]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
I think you should use XmlIncludeAttribute like this:
[XmlInclude(typeof(Element1))]
public class Element
{
}
Here is xml and code. I like to first serialize with test data, then deserialize.
<?xml version="1.0" encoding="utf-8"?>
<Root1>
<name>Name1</name>
<company>Comp1</company>
<url>site.com</url>
<element d2p1:type="Element1" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site1.com</url>
<price>15000</price>
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
</element>
<element d2p1:type="Element2" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site2.com</url>
<price>100</price>
<language>lg</language>
<binding>123</binding>
</element>
</Root1>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string FILENAME = #"c:\temp\test.xml";
Root1 root1 = new Root1() {
Name = "Name1",
Company = "Comp1",
Url = "site.com",
ElementList = new List<Element>() {
new Element1() {
Url = "site1.com",
Price = "15000",
mw = "true",
co = "Япония"
},
new Element2() {
Url = "site2.com",
Price = "100",
lg = "lg",
bind = "123"
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Root1));
StreamWriter writer = new StreamWriter(FILENAME);
XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
_ns.Add("", "");
serializer.Serialize(writer, root1, _ns);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Root1));
XmlTextReader reader = new XmlTextReader(FILENAME);
Root1 newRoot1 = (Root1)xs.Deserialize(reader);
}
}
[XmlRoot("Root1")]
public class Root1
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("element")]
public List<Element> ElementList { get; set; }
}
[XmlInclude(typeof(Element1))]
[XmlInclude(typeof(Element2))]
[XmlRoot("element")]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
}
[XmlRoot("element1")]
public class Element1 : Element
{
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
}
[XmlRoot("element2")]
public class Element2 : Element
{
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
}
Code below matches better with your posted XML. You need to compare the generated xml with your xml.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string FILENAME = #"c:\temp\test.xml";
Root1 root1 = new Root1()
{
Name = "Name1",
Company = "Comp1",
Url = "site.com",
cElement = new Elements() {
ElementList = new List<Element>() {
new Element1() {
Url = "site1.com",
Price = "15000",
mw = "true",
co = "Япония"
},
new Element2() {
Url = "site2.com",
Price = "100",
lg = "lg",
bind = "123"
}
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Root1));
StreamWriter writer = new StreamWriter(FILENAME);
XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
_ns.Add("", "");
serializer.Serialize(writer, root1, _ns);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Root1));
XmlTextReader reader = new XmlTextReader(FILENAME);
Root1 newRoot1 = (Root1)xs.Deserialize(reader);
}
}
[XmlRoot("Root1")]
public class Root1
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("elements")]
public Elements cElement { get; set; }
}
[XmlRoot("elements")]
public class Elements
{
[XmlElement("element")]
public List<Element> ElementList { get; set; }
}
[XmlInclude(typeof(Element1))]
[XmlInclude(typeof(Element2))]
[XmlRoot("element", Namespace = "")]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
}
[XmlRoot("element1", Namespace = "")]
public class Element1 : Element
{
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
}
[XmlRoot("element2", Namespace = "")]
public class Element2 : Element
{
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
}​

Deserialize XML with multiple namespaces to objects

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&apos; (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.

Categories