How to create XML file from List in ASP .NET and in C# using LINQ Example/Sample code

How to create XML file from List in ASP .NET and in C# using LINQ  Example/Sample code

ASP.NET and C# sample code:



Creating the following XML file in C# .NET Using LINQ
<Books BookType="TECH">
  <Book Code="123" name="ASP.NET" />
  <Book Code="123" name="C#.NET" />
</Request>

List<string> items = new List<string>();

//Fill the list items
-----------
-----------

XElement xmlFile = new XElement(XName.Get("Books"), new XAttribute("BookType ", "TECH"),
                  from i in items
select new XElement(XName.Get("Book"), new XAttribute("Code", "123"),
                         new XAttribute("name", i)
                                                ));

XDocument xdoc = new XDocument(new XElement(xmlFile));

No comments:

Post a Comment