EDN Admin
Well-known member
Hello,
<br/>
Ive got a typical hiearchy in my database of which the simple definition looks like this:
<br/>
<br/>
<pre>EntityId (PK)
Name
ParentId (Reference to the PK)[/code]
<br/>
<br/>
As you can see its a self-referencing table. My simple class-equivalent looks like this:
<br/>
<div style="color:Black;background-color:White
<pre> [Serializable]
<span style="color:Blue public <span style="color:Blue class Entity
{
[XmlAttribute]
<span style="color:Blue public <span style="color:Blue int EntityId { <span style="color:Blue get; <span style="color:Blue set; }
[XmlAttribute]
<span style="color:Blue public <span style="color:Blue string Name { <span style="color:Blue get; <span style="color:Blue set; }
[XmlIgnore]
<span style="color:Blue public <span style="color:Blue int ParentId { <span style="color:Blue get; <span style="color:Blue set; }
}
[/code]
<br/>
I currently have two ways to represent a set of data in .NET:
<br/>
<ol>
Using recursion, fill up a Dictionary where each ParentId is the key with a List of its children.
<div style="color:Black;background-color:White
<pre>Dictionary<<span style="color:Blue int, List<Entity>>
[/code]
Just fetch all the data in for example a List.
<div style="color:Black;background-color:White
<pre>List<Entity>
[/code]
</ol>
<br/>
I want to serialize the Entity classes (for the record: this is a superclass) to XML in a nested structure. Example data:
<br/>
<pre> <span style="text-decoration:underline EntityId <span style="text-decoration:underline Name <span style="text-decoration:underline ParentId<span style="font-weight:normal
----------|----------| ---------|
1 | foo | NULL |
2 | bar | 1 |
3 | fish | 2 |
4 | food | 2 |
5 | tomato | 1 |[/code]
<br/>
Should serialize to this XML structure:
<br/>
<div style="color:Black;background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 1<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue foo<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 2<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue bar<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 3<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue fish<span style="color:Black " <span style="color:Blue />
<span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 4<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue food<span style="color:Black " <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:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 5<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue tomato<span style="color:Black " <span style="color:Blue />
<span style="color:Blue </<span style="color:#A31515 Entity<span style="color:Blue >
[/code]
<br/>
<br/>
This is where Im stuck: I dont know if this can be done in a flexible way... The XML-serializers wont be able to detect this kind of hierarchy. As you can see the classes arent related in a "proper" object-oriented way, instead I have a parent-children structure that I put in either a Dictionary or a flat List.
<br/>
Does this mean I need to build this up manually with the XML-DOM classes in .NET or can anyone suggest me a good approach and/or provide me examples?
<br/>
Thanks in advance, help will be greatly appreciated!
Progger
View the full article
<br/>
Ive got a typical hiearchy in my database of which the simple definition looks like this:
<br/>
<br/>
<pre>EntityId (PK)
Name
ParentId (Reference to the PK)[/code]
<br/>
<br/>
As you can see its a self-referencing table. My simple class-equivalent looks like this:
<br/>
<div style="color:Black;background-color:White
<pre> [Serializable]
<span style="color:Blue public <span style="color:Blue class Entity
{
[XmlAttribute]
<span style="color:Blue public <span style="color:Blue int EntityId { <span style="color:Blue get; <span style="color:Blue set; }
[XmlAttribute]
<span style="color:Blue public <span style="color:Blue string Name { <span style="color:Blue get; <span style="color:Blue set; }
[XmlIgnore]
<span style="color:Blue public <span style="color:Blue int ParentId { <span style="color:Blue get; <span style="color:Blue set; }
}
[/code]
<br/>
I currently have two ways to represent a set of data in .NET:
<br/>
<ol>
Using recursion, fill up a Dictionary where each ParentId is the key with a List of its children.
<div style="color:Black;background-color:White
<pre>Dictionary<<span style="color:Blue int, List<Entity>>
[/code]
Just fetch all the data in for example a List.
<div style="color:Black;background-color:White
<pre>List<Entity>
[/code]
</ol>
<br/>
I want to serialize the Entity classes (for the record: this is a superclass) to XML in a nested structure. Example data:
<br/>
<pre> <span style="text-decoration:underline EntityId <span style="text-decoration:underline Name <span style="text-decoration:underline ParentId<span style="font-weight:normal
----------|----------| ---------|
1 | foo | NULL |
2 | bar | 1 |
3 | fish | 2 |
4 | food | 2 |
5 | tomato | 1 |[/code]
<br/>
Should serialize to this XML structure:
<br/>
<div style="color:Black;background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 1<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue foo<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 2<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue bar<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 3<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue fish<span style="color:Black " <span style="color:Blue />
<span style="color:Blue <<span style="color:#A31515 Entity <span style="color:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 4<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue food<span style="color:Black " <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:Red Id<span style="color:Blue =<span style="color:Black "<span style="color:Blue 5<span style="color:Black " <span style="color:Red Name<span style="color:Blue =<span style="color:Black "<span style="color:Blue tomato<span style="color:Black " <span style="color:Blue />
<span style="color:Blue </<span style="color:#A31515 Entity<span style="color:Blue >
[/code]
<br/>
<br/>
This is where Im stuck: I dont know if this can be done in a flexible way... The XML-serializers wont be able to detect this kind of hierarchy. As you can see the classes arent related in a "proper" object-oriented way, instead I have a parent-children structure that I put in either a Dictionary or a flat List.
<br/>
Does this mean I need to build this up manually with the XML-DOM classes in .NET or can anyone suggest me a good approach and/or provide me examples?
<br/>
Thanks in advance, help will be greatly appreciated!
Progger
View the full article