How to Complete the Xelement Class and Get the Result Below

advertisements

Using a loop with xelement class in C# i would like to get the below result!

<data>

 <description>Cities that I have recently visited.</description>

 <cities>
  <city id="1">
   <name>Chicago1</name>
   <state>IN1</state>
  </city>

  <city id="2">
   <name>Chicago2</name>
   <state>IN2</state>
  </city>

  <city id="3">
   <name>Chicago3</name>
   <state>IN3</state>
  </city>

 </cities>

 </data>

This is the code i have tried so far! any help?? i need to use a loop and get the above values..The loop i used is commented..

namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement xmlDataStore = new XElement("data",
            new XElement("cities",

    new XElement("city", new XAttribute("id", "1")),
    new XElement("city", "Colombo"),
    new XElement("name", "lname"),
    new XElement("state", "0772569984")

    )
            )
                ;

            //var list = from x in XElement.ReadFrom(xmlDataStore).Element("Node").Elements()
            //select new
            //{
            //   Name = x.Name,
            //   Value = (string)x
            //};

            Console.WriteLine(xmlDataStore);
            Console.ReadLine();

        }

    }
}

What i get...

<cities>
  <city id="1">
   <name>Chicago1</name>
   <state>IN1</state>
  </city>
  </cities>

What i want...

<data>

 <description>Cities that I have recently visited.</description>

 <cities>
  <city id="1">
   <name>Chicago1</name>
   <state>IN1</state>
  </city>

  <city id="2">
   <name>Chicago2</name>
   <state>IN2</state>
  </city>

  <city id="3">
   <name>Chicago3</name>
   <state>IN3</state>
  </city>

 </cities>

 </data>


Okay, I still don't exactly know what the real problem is, so let's start with this:

private static void citiesXml()
{
  const string desc = "Cities that I have recently visited.";

  // set up a list of all the different cities
  var list = new List<Tuple<string, string>>();
  list.Add(new Tuple<string, string>("Chicago1", "IN1"));
  list.Add(new Tuple<string, string>("Chicago2", "IN2"));
  list.Add(new Tuple<string, string>("Chicago3", "IN3"));

  var xmlDataStore = new XElement("data", new XElement("description", desc));
  var xmlCities = new XElement("cities");

  // loop through the list of cities and create a XElement for each single one
  for (var i = 0; i < list.Count; i++)
  {
    xmlCities.Add(new XElement("city",
        new XAttribute("id", i + 1),
        new XElement("name", list[i].Item1),
        new XElement("state", list[i].Item2)));
  }

  // add the cities to the data store object
  xmlDataStore.Add(xmlCities);

  Console.WriteLine(xmlDataStore);
  Console.ReadLine();
}

This will print:

<data>
  <description>Cities that I have recently visited.</description>
  <cities>
    <city id="1">
      <name>Chicago1</name>
      <state>IN1</state>
    </city>
    <city id="2">
      <name>Chicago2</name>
      <state>IN2</state>
    </city>
    <city id="3">
      <name>Chicago3</name>
      <state>IN3</state>
    </city>
  </cities>
</data>

So as far as I see it, the only difference is that there are no blank lines between cities. Are the missing blank lines the problem?