Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using a loop with xelement class in C# i would like to get the below result!

XML
<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..

C#
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")

    )    
            )
                ;
                   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();

        }


    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900