problem getting elements after serialization

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi.
I have a problem extracting elements from a XML file after serializing a complex object in it.
Im using DataContractSerializer in order to serialize the object.
The code snippet below:

<div style="background-color:white; color:black
<pre><span style="color:blue namespace SerDiser
{
<span style="color:blue public <span style="color:blue class Entity
{
<span style="color:blue public Guid ID { <span style="color:blue get; <span style="color:blue set; }
<span style="color:blue public <span style="color:blue string Name { <span style="color:blue get; <span style="color:blue set; }
}

<span style="color:blue public <span style="color:blue class ListEntities
{
<span style="color:blue private List<Entity> entities;
<span style="color:gray /// <span style="color:gray <summary>
<span style="color:gray ///<span style="color:green Audits to Do
<span style="color:gray /// <span style="color:gray </summary>
<span style="color:blue public List<Entity> Entities
{
<span style="color:blue get
{
<span style="color:blue if (entities == <span style="color:blue null)
entities = <span style="color:blue new List<Entity>();

<span style="color:blue return entities;
}
<span style="color:blue set
{
entities = value;
}
}
<span style="color:blue public <span style="color:blue string Name { <span style="color:blue get; <span style="color:blue set; }
}

<span style="color:blue class Program
{
<span style="color:blue static <span style="color:blue void Main(<span style="color:blue string[] args)
{

ListEntities entitiesList = <span style="color:blue new ListEntities();
List<Entity> list = <span style="color:blue new List<Entity>() { <span style="color:blue new Entity() { ID = Guid.NewGuid(), Name = <span style="color:#a31515 "Bob" }, <span style="color:blue new Entity() { ID = Guid.NewGuid(), Name = <span style="color:#a31515 "John" }, <span style="color:blue new Entity() { ID = Guid.NewGuid(), Name = <span style="color:#a31515 "Brad" }, <span style="color:blue new Entity() { ID = Guid.NewGuid(), Name = <span style="color:#a31515 "Donald" }, <span style="color:blue new Entity() { ID = Guid.NewGuid(), Name = <span style="color:#a31515 "Mike" } };
entitiesList.Entities.AddRange(list);

<span style="color:blue using (Stream stream = File.Create(<span style="color:#a31515 @"C:UsersTeamDesktoptest.xml"))
{
DataContractSerializer xmlser = <span style="color:blue new DataContractSerializer(<span style="color:blue typeof(ListEntities));
<span style="color:green //XmlSerializer xmlser = new XmlSerializer(typeof(ListEntities));

xmlser.WriteObject(stream, entitiesList);
<span style="color:green //xmlser.Serialize(stream, entitiesList);
}

XDocument document;
Stream filestream = File.OpenRead(<span style="color:#a31515 @"C:UsersTeamDesktoptest.xml");
<span style="color:blue using (<span style="color:blue var reader = <span style="color:blue new StreamReader(filestream))
{
<span style="color:blue string payload = reader.ReadToEnd().Replace(<span style="color:#a31515 , <span style="color:#a31515 );
document = XDocument.Parse(payload);
XElement root = document.Root;
XElement xelement = root.Element(<span style="color:#a31515 "Entities");
}
}
}
}

[/code]


The resulting file has the form:

<div style="background-color:white; color:black
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black "<span style="color:blue ?>
<span style="color:blue <<span style="color:#a31515 ListEntities <span style="color:red xmlns:xsi<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema-instance<span style="color:black " <span style="color:red xmlns:xsd<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Entities<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ID<span style="color:blue >bee3abcd-c619-425d-be74-4890dd97ea7d<span style="color:blue </<span style="color:#a31515 ID<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Name<span style="color:blue >Bob<span style="color:blue </<span style="color:#a31515 Name<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ID<span style="color:blue >b89097b8-72b3-4449-afa7-fe139a186e24<span style="color:blue </<span style="color:#a31515 ID<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Name<span style="color:blue >John<span style="color:blue </<span style="color:#a31515 Name<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ID<span style="color:blue >a1f0a91f-b546-4e01-8ff6-45ab5baf6c92<span style="color:blue </<span style="color:#a31515 ID<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Name<span style="color:blue >Brad<span style="color:blue </<span style="color:#a31515 Name<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ID<span style="color:blue >7caaafb3-39d7-42ac-9cd5-1130bdc8242f<span style="color:blue </<span style="color:#a31515 ID<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Name<span style="color:blue >Donald<span style="color:blue </<span style="color:#a31515 Name<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ID<span style="color:blue >83f2af5a-fec0-4f64-b593-c9e77a634972<span style="color:blue </<span style="color:#a31515 ID<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Name<span style="color:blue >Mike<span style="color:blue </<span style="color:#a31515 Name<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Entity<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Entities<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 ListEntities<span style="color:blue >
[/code]

Now xelement in XElement xelement = root.Element("Entities") is NULL for some reasons.
But gets the right value when I use XmlSerializer instead of DataContractSerializer.
The reason I want to use DataContractSerializer is because of serialization of nullalbe properties(nil attribute), which XmlSerializer doesnt make.
So may be someone knows the reasons for this issue.
Thanks and regards.

View the full article
 
Back
Top