In this example I will show you how to create xml document using Linq .In order to extract data from a database and create an xml document we need to include the namespace using System.Xml.Linq. The important class with the Linq to Xml hierarchy XElements and XDocument, XElement class represents an XML element node that contains child elements as child nodes.The output xml file will be saved at a specified location of the project folder.
This example I used northwind sample database for more detail about how to download click link button it will show a popup.
Example:
protected void Page_Load(object sender, EventArgs e)
{
models db = new models();
var customerOrders = from customer in db.Customers
where customer.ContactName.StartsWith("C") && customer.Orders.Count> 0
orderby customer.ContactName
select customer;
var xml = new XElement("customers", from customer in customerOrders.ToList()
select new XElement("ContactName",
new XAttribute("ContactName", customer.ContactName), new XAttribute("phone", customer.Phone)));
var path = Path.Combine(Server.MapPath("~/Xmlfiles"), "file1" + ".xml");
System.IO.File.WriteAllText(path,xml.ToString());
}
Output:
Post your comments / questions
Recent Article
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
Related Article