Nested XML Serialization C #
I need to create nested Xml with serialization. I have two lists with data that should generate nested xml filtered by Number.
Lists:
List<Person> personList = new List<Person>();
personList.Add(new Person{
Number = 1,
Name = "Dean"
});
personList.Add(new Person{
Number = 2,
Name = "Mike"
});
List<Home> homeList= new List<Home>();
homeList.Add(new Home{
Number = 2,
City= "Paris",
State = "France"
});
homeList.Add(new Home{
Number = 1,
City= "London",
State = "England"
});
So, I have a class that I am using to serialize:
public class CreateXML
{
[XElement(ElementName = "Home")]
List<Home> homeList= new List<Home>();
[XElement(ElementName = "Person")]
List<Person> personList = new List<Person>();
}
XML generation way:
public void Serialize(CreateXML list)
{
XmlSerializer serializer = new XmlSerializer(typeof(CreateXML));
using (TextWriter writer = new StreamWriter(@"D:\XmlTEST.txt"))
{
serializer.Serialize(writer, list);
}
}
Now it generates an Xml file first with all Person data and then with Home data.
Actual output:
<Person>
<number>1<number>
<name>Dean</name>
</Person>
<Person>
<number>2<number>
<name>Mike</name>
</Person>
<Home>
<number>1</number>
<city>London</city>
<state>England</state>
</Home>
<number>2</number>
<city>Paris</city>
<state>London</state>
</Home>
Output Required:
<Person>
<number>1<number>
<name>Dean</name>
<Home>
<number>1</number>
<city>London</city>
<state>England</state>
</Home>
</Person>
<Person>
<number>2<number>
<name>Mike</name>
<Home>
<number>2</number>
<city>Paris</city>
<state>France</state>
</Home>
</Person>
Any suggestions?
source to share
You want to associate a specific Home
with Person
Dean:
<Person>
<number>1</number>
<name>Dean</name>
<Home>
<number>1</number>
<city>London</city>
<state>England</state>
</Home>
</Person>
However, in your code, you are not linking these two objects:
personList.Add(new Person {
Number = 1,
Name = "Dean"
});
homeList.Add(new Home{
Number = 1,
City= "London",
State = "England"
});
In the example above, the entities are not completely linked - you need to link them somehow.
One way you can bind these rights is by defining a property Home
on the type Person
:
public class Person
{
public string Name { get; set; }
public int Number { get; set; }
public Home Home { get; set; }
}
Then click homeList
and assign instances to Home
instances Person
:
List<Person> personList = new List<Person>();
personList.Add(new Person
{
Number = 1,
Name = "Dean",
Home = new Home
{
Number = 1,
City = "London",
State = "England"
}
});
Output:
<personList>
<Person>
<Name>Dean</Name>
<Number>1</Number>
<Home>
<Number>2</Number>
<City>Paris</City>
<State>France</State>
</Home>
</Person>
<Person>
<Name>Mike</Name>
<Number>2</Number>
<Home>
<Number>1</Number>
<City>London</City>
<State>England</State>
</Home>
</Person>
</personList>
Update
Based on your comment, you can dynamically assign the property Home
like this:
foreach (var home in homeList)
{
foreach (var person in personList)
{
if (home.Number == person.Number)
{
person.Home = home;
}
}
}
Do not forget that if the corresponding house number is missing, the property House
will remain null
.
source to share