How to fill DTO using XmlSerializer.Deserialize

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im using XmlSerializer.Deserialize to create data transfer objects from XML. Because of the XMLs structure (see below) I built my list classes like this:
public class CategoriesCursor<br/>
{<br/>
public new ObservableCollection<Categories> Items;<br/>
}
But Id rather have them like this:
public class CategoriesCursor<Categories><br/>
{<br/>
}
In other words, I want my DTO to be the ObservableCollection instead of having an ObservableCollection called Items inside my DTO. Is there a way to set the XmlRoot and/or XmlElement attributes differently to make this work?
Below is sample XML and working sample code.
TIA,
Markus

<div style="color:Black;background-color:White; <pre>
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<span style="color:Blue; <<span style="color:#A31515; VFPDataSet<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; CategoriesView<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; categoryid<span style="color:Blue; >1<span style="color:Blue; </<span style="color:#A31515; categoryid<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; categoryname<span style="color:Blue; >Beverages<span style="color:Blue; </<span style="color:#A31515; categoryname<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Soft drinks, coffees, teas, beers, and ales<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; CategoriesView<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; CategoriesView<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; categoryid<span style="color:Blue; >2<span style="color:Blue; </<span style="color:#A31515; categoryid<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; categoryname<span style="color:Blue; >Condiments<span style="color:Blue; </<span style="color:#A31515; categoryname<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Sweet and savory sauces, relishes, spreads, and seasonings<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; CategoriesView<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; CategoriesView<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; categoryid<span style="color:Blue; >3<span style="color:Blue; </<span style="color:#A31515; categoryid<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; categoryname<span style="color:Blue; >Confections<span style="color:Blue; </<span style="color:#A31515; categoryname<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Desserts, candies, and sweet breads<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; CategoriesView<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; VFPDataSet<span style="color:Blue; >

[/code]
This is working sample code:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System.Collections.ObjectModel;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.ComponentModel.DataAnnotations;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Xml;
<span style="color:Blue; using System.Xml.Serialization;

<span style="color:Blue; namespace XmlSerializerTest
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
<span style="color:Blue; string xml = File.OpenText(<span style="color:#A31515; "CategoriesView.xml").ReadToEnd();
CategoriesCursor categoriesView = CreateCursor<CategoriesCursor>(xml);
}

<span style="color:Blue; public <span style="color:Blue; static T CreateCursor<T>(<span style="color:Blue; string xml)
{
XmlSerializer xmlSerializer = <span style="color:Blue; new XmlSerializer(<span style="color:Blue; typeof(T));
StringReader stringReader = <span style="color:Blue; new StringReader(xml);
XmlReader xmlTextReader = XmlReader.Create(stringReader);
T dto = (T)xmlSerializer.Deserialize(xmlTextReader);
<span style="color:Blue; return dto;
}
}

[XmlRoot(<span style="color:#A31515; "VFPDataSet")]
<span style="color:Blue; public <span style="color:Blue; class CategoriesCursor : Cursor<Categories>
{
[XmlElement(<span style="color:#A31515; "CategoriesView")]
<span style="color:Blue; public <span style="color:Blue; new ObservableCollection<Categories> Items
{
<span style="color:Blue; get { <span style="color:Blue; return <span style="color:Blue; base.Items; }
<span style="color:Blue; set { <span style="color:Blue; base.Items = value; }
}
}

[XmlRoot(<span style="color:#A31515; "CategoriesView")]
<span style="color:Blue; public <span style="color:Blue; class Categories : Record
{
[XmlElement(<span style="color:#A31515; "categoryid")]
<span style="color:Blue; public <span style="color:Blue; int Categoryid { <span style="color:Blue; get; <span style="color:Blue; set; }

[XmlElement(<span style="color:#A31515; "categoryname")]
<span style="color:Blue; public <span style="color:Blue; string Categoryname { <span style="color:Blue; get; <span style="color:Blue; set; }

[XmlElement(<span style="color:#A31515; "description")]
<span style="color:Blue; public <span style="color:Blue; string Description { <span style="color:Blue; get; <span style="color:Blue; set; }
}

<span style="color:Blue; public <span style="color:Blue; class Cursor<T> : INotifyPropertyChanged <span style="color:Blue; where T : Record
{
<span style="color:Blue; protected ObservableCollection<T> items = <span style="color:Blue; new ObservableCollection<T>();
<span style="color:Blue; protected <span style="color:Blue; virtual ObservableCollection<T> Items
{
<span style="color:Blue; get { <span style="color:Blue; return items; }
<span style="color:Blue; set
{
items = value;
RaisePropertyChanged(<span style="color:#A31515; "Items");
}
}

<span style="color:Blue; public <span style="color:Blue; event PropertyChangedEventHandler PropertyChanged;
<span style="color:Blue; protected <span style="color:Blue; void RaisePropertyChanged(<span style="color:Blue; string propertyName)
{
PropertyChangedEventHandler propertyChanged = <span style="color:Blue; this.PropertyChanged;
<span style="color:Blue; if (propertyChanged != <span style="color:Blue; null)
{
propertyChanged(<span style="color:Blue; this, <span style="color:Blue; new PropertyChangedEventArgs(propertyName));
}
}

}

<span style="color:Blue; public <span style="color:Blue; class Record : INotifyPropertyChanged
{
<span style="color:Blue; public <span style="color:Blue; event PropertyChangedEventHandler PropertyChanged;
<span style="color:Blue; protected <span style="color:Blue; void RaisePropertyChanged(<span style="color:Blue; string propertyName)
{
PropertyChangedEventHandler propertyChanged = <span style="color:Blue; this.PropertyChanged;
<span style="color:Blue; if (propertyChanged != <span style="color:Blue; null)
{
propertyChanged(<span style="color:Blue; this, <span style="color:Blue; new PropertyChangedEventArgs(propertyName));
}
}
}
}

[/code]
<br/>

View the full article
 
Back
Top