Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
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")
 
    )    
            )
                ;
                   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 25-Jul-13 23:41pm

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



Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | Last Updated 26 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100