LINQ to XML : XElement and XAttribute
var _CountryLists = (from c in SW_Countries select new { c.ID, c.Name, c.Abbreviation });
var xml = new XElement("root",from c in _CountryLists
select new XElement("nodes",
new XAttribute("id", c.ID),
new XAttribute("abb", c.Abbreviation),
new XAttribute("name", c.Name)));
return xml.ToString();
xml will return result like this...
<root>
<nodes id="13" abb="AUS" name="Australia" />
<nodes id="28" abb="MMR" name="Burma" />
<nodes id="33" abb="BRA" name="Brazil" />
<nodes id="35" abb="BTN" name="Bhutan" />
<nodes id="40" abb="CAN" name="Canada" />
<nodes id="43" abb="LKA" name="Sri Lanka" />
<nodes id="46" abb="CHN" name="China" />
<nodes id="67" abb="EGY" name="Egypt" />
<nodes id="68" abb="IRL" name="Ireland" />
<nodes id="87" abb="GAB" name="Gabon" />
<nodes id="113" abb="IND" name="India" />
<nodes id="116" abb="IRN" name="Iran" />
<nodes id="177" abb="NPL" name="Nepal" />
</root>
var xml = new XElement("root",from c in _CountryLists
select new XElement("nodes",
new XAttribute("id", c.ID),
new XAttribute("abb", c.Abbreviation),
new XAttribute("name", c.Name)));
return xml.ToString();
xml will return result like this...
<root>
<nodes id="13" abb="AUS" name="Australia" />
<nodes id="28" abb="MMR" name="Burma" />
<nodes id="33" abb="BRA" name="Brazil" />
<nodes id="35" abb="BTN" name="Bhutan" />
<nodes id="40" abb="CAN" name="Canada" />
<nodes id="43" abb="LKA" name="Sri Lanka" />
<nodes id="46" abb="CHN" name="China" />
<nodes id="67" abb="EGY" name="Egypt" />
<nodes id="68" abb="IRL" name="Ireland" />
<nodes id="87" abb="GAB" name="Gabon" />
<nodes id="113" abb="IND" name="India" />
<nodes id="116" abb="IRN" name="Iran" />
<nodes id="177" abb="NPL" name="Nepal" />
</root>
Comments