3

I need to create nested Xml with serialization. I have two Lists with data that have to generate nested xml filtered by Number.

The 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 next i have class that i use for serialization:

public class CreateXML
{
[XElement(ElementName = "Home")]
List<Home> homeList= new List<Home>();
[XElement(ElementName = "Person")]
List<Person> personList = new List<Person>();
}

Method for creating XML:

 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 Xml file first with all Person data 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>

Wanted ouput:

<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?

2
  • What is the difference between the desired and actual output? Commented Jul 28, 2015 at 11:32
  • if you want to have an element Home inside the Person at the output xml structure, you need to add field with type Home to Person class. Or with what you stuck? Commented Jul 28, 2015 at 11:33

1 Answer 1

3

You want to associate a certain Home with the Person Dean: 

<Person>
  <number>1</number>
  <name>Dean</name>
  <Home>
    <number>1</number>
    <city>London</city>
    <state>England</state>
  </Home>
</Person>

Yet in your code you do not associate these two entities:

personList.Add(new Person {
    Number = 1,
    Name = "Dean"
});

homeList.Add(new Home{
    Number = 1,
    City= "London",
    State = "England"
});

In the above example entities are completely unrelated -- you need to somehow relate them.

One way you can relate these entites is by defining a Home property on the Person type:

public class Person
{
    public string Name { get; set; }
    public int Number { get; set; }
    public Home Home { get; set; }
}

Then ditch homeList and assign Home instances to Person instances:

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 to the Home property like this:

foreach (var home in homeList)
{
    foreach (var person in personList)
    {
        if (home.Number == person.Number)
        {
            person.Home = home;
        }
    }
}

Bare in mind that, if there is no corresponding house number, the House property will remain null.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes that's what i want to do, but want to filter the result, not to do hardcoded. Example Home = new Home{ if(Home.Number == Person.Number } then to list all the data.
@PetarS Updated my answer. Is that what you are looking for?
that's what i looking for, i was trying to do the same thing all day, but still having problems where to put the foreach statement.
@PetarS Awesome. You can put it beneath your personList and homeList variables. If this answered your question, please mark it as accepted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.