You must Sign In to post a response.
  • Category: XML

    How to generate XML in asp.net?


    Are you looking for information on XML generation? Want to know how to generate XML in asp.net? Read this thread to learn more about XML generation with examples.



    Hi experts,
    I want to generate an XML file from sql server Database such as one can read the data from that xml file on an specific id. how can I generate xml file in asp.net page.
  • #729884
    Hai Sajib Barua,
    If you have your data in the DataBase and you want to get it as an XML file then you first need to get the data in dataset.for the dataset, we have the property called GetXML() which will convert the dataset values to XML format.
    See below the code snippet:

    DataSet ds= new DataSet();
    ds= GetDataSetFromDataBase();
    ds.GetXML();

    for more details, you can go through the below link:
    http://msdn.microsoft.com/en-us/library/system.data.dataset.getxml.aspx

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #729885
    If you need to create XML files in PHP, you can do it.Try to change code according to your need
    <?
    $doc = new_xmldoc('1.0');
    $root = $doc->add_root('Employees');
    $Employee = $root->new_child('Employee','');

    $Employee->new_child('lName','Mohan');
    $Employee->new_child('fName','Kumar');
    $Employee->new_child('contribution','3500');

    $Employee = $root->new_child('Employee','');

    $Employee->new_child('lName','Naveen');
    $Employee->new_child('fName','Kumar);
    $Employee->new_child('contribution','1400');

    $Employee = $root->new_child('Employee','');

    $fp = @fopen('Employees.xml','w');
    if(!$fp) {
    die('Error cannot create XML file');
    }
    fwrite($fp,$doc->dumpmem());
    fclose($fp);
    ?>
    Marks as answer if helped you!

  • #729915
    As we know XML has customized structure so you can write and define your xml structure as per need.
    check below to create a xml
    you can use XML writer class to write xml.

    using (XmlWriter writer = XmlWriter.Create("employees.xml"))
    {
    writer.WriteStartDocument();
    writer.WriteStartElement("Employees"); // <-- Important root element
    writer.WriteElementString("ID","10");
    writer.WriteEndElement(); // <-- Closes it
    writer.WriteEndDocument();
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #729939
    Using the code you can create the XML file as follows


    public void createXML()
    {
    XmlDocument doc = new XmlDocument();


    string filename="c:\\sample.xml";
    XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents the Encoding Type//
    tw.Formatting=Formatting.Indented; //for xml tags to be indented//

    //tw.WriteStartDocument(); //Indicates the starting of document (Required)//


    //tw.WriteStartElement("Employees");
    //tw.WriteStartElement("Employee","Genius");
    //tw.WriteAttributeString("Name","krishnan");
    //tw.WriteElementString("Designation","Software Developer");
    //tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");

    tw.WriteStartElement("Author");
    tw.WriteElementString("ID","1");
    tw.WriteElementString("SubmissionDate","1-1-08");
    tw.WriteElementString("Topic","test");
    tw.WriteElementString("Title","test");
    tw.WriteStartElement("Authors");
    tw.WriteStartElement("Author");
    tw.WriteElementString("Name","XYZ");
    tw.WriteElementString("Affiliation","ABC");
    tw.WriteEndElement();
    tw.WriteEndElement();
    tw.WriteElementString("Summary","Hi");
    tw.WriteElementString("Text","Hello");

    tw.WriteEndElement();
    //tw.WriteEndDocument();
    tw.Flush();
    tw.Close();

    StreamReader srXml = new StreamReader(filename);
    string xmlFile = srXml.ReadToEnd();

    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #729948
    Hi Sajib Barua,

    Pls verify the below URL.

    http://www.dotnetgallery.com/forum/thread45-How-to-generate-XML-in-aspnet.aspx

  • #729949
    using System.Xml.Linq;
    ...
    ...
    ...

    XDocument xmlDoc = new XDocument(
    new XDeclaration("1.0", "utf-16", "true"),
    new XElement("source",
    new XElement("publisher","Super X Job Site"),
    new XElement("publisherurl","http://www.superxjobsite.com")
    )
    );
    foreach (Job job in jobs)
    {
    xmlDoc.Element("source").Add(
    new XElement("job",
    new XElement("title", new XCData(job.Title)),
    new XElement("date", new XCData(job.Date.ToShortDateString())),
    new XElement("referencenumber", new XCData(job.ReferenceNumber)),
    new XElement("url", new XCData(job.Url)),
    new XElement("company", new XCData(job.Company)),
    new XElement("city", new XCData(job.City)),
    new XElement("country", new XCData(job.Country)),
    new XElement("description", new XCData(job.Description))
    )
    );
    }


    Thanks,
    ketan

  • #729961
    see below code:

    static void Main(string[] args)
    {
    Console.WriteLine("Enter Employee detail\n\n");
    Console.WriteLine("Enter ID:");
    string id = Console.ReadLine();
    Console.WriteLine("Enter NAME:");
    string name = Console.ReadLine();
    Console.WriteLine("Enter DEPARTMENT:");
    string dept = Console.ReadLine();
    Console.WriteLine("Enter DATE OF BIRTH:");
    Console.WriteLine("Enter DAY:");
    string dd = Console.ReadLine();
    Console.WriteLine("Enter MONTH:");
    string mm = Console.ReadLine();
    Console.WriteLine("Enter YEAR:");
    string yy = Console.ReadLine();

    //MyXmlWrite(id, name, dept, dd, mm, yy);

    // Console.WriteLine("XML File is generated");
    AppendXml( id, name, dept, dd, mm, yy);

    MyReadXml();


    Console.ReadLine();

    }

    public static void MyReadXml()
    {
    FileStream fs = new FileStream("z:\\Employee.xml", FileMode.OpenOrCreate, FileAccess.Read);
    XmlTextReader xr = new XmlTextReader(fs);
    xr.WhitespaceHandling = WhitespaceHandling.None;
    while (xr.Read())
    {
    switch (xr.NodeType)
    {
    case XmlNodeType.Element:
    Console.WriteLine(xr.Name + ":-");
    if (xr.HasAttributes)
    {

    while (xr.MoveToNextAttribute())
    {
    Console.WriteLine(xr.Name + ":-\t" + xr.Value);
    }

    xr.MoveToElement();
    }
    break;
    case XmlNodeType.Text:
    Console.WriteLine("\t" + xr.Value);
    break;

    }
    }
    xr.Close();
    fs.Close();
    }

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.