Create complete xml string using LINQ

Madhu.b.rokkam
Posted by Madhu.b.rokkam under LINQ category on | Points: 40 | Views : 1944
When ever there is a need to create an xml string with attributes and elements you can do this way

XElement xml = new XElement("Students",
new XElement("Madhu",
new XAttribute("YearOfBirth", 1990),
new XElement("Location", "Hyderabad")),
new XElement("Karthik",
new XAttribute("YearOfBirth", 1995),
new XElement("Location", "Chennai"))
);

MessageBox.Show(xml.ToString());

This will result output as

<Students>
<Madhu YearOfBirth="1990">
<Location>Hyderabad</Location>
</Madhu>
<Karthik YearOfBirth="1995">
<Location>Chennai</Location>
</Karthik>
</Students>

Comments or Responses

Login to post response