Generating XML file in C# from SQL Database

Last post 07-04-2009 3:27 AM by vohrasohilhis. 3 replies.

Sort Posts:

  • Generating XML file in C# from SQL Database

    10-23-2008, 8:18 PM
    • Member
      4 point Member
    • zafarjaffary
    • Member since 10-19-2008, 4:21 PM
    • Posts 42

    HI

    I am trying to Create a XML file in Visual Studio Using C sharp. I am using Customer table of Northwind database. Can someone help me out because i could not find what i am looking for. Below is my requirement.

    A:
    I want to create a c# GetCustomerXML method which have parameter to be pass to my Select statement in the WHERE clause and Generate the XML output file on my hard disk.

    For Example
    GetCustomer(String Country) Country will be use as parameter in Where clause sothat it will return whatever country have customers.

    B:
    At the end when xml file created another statement runs and add some more line at the bottom if i want Company name if they belong to some other city(Like London). See at end of XML file Let say my choice is london it will have line at bottom saying these are the company name.

    <customer>
    <
    CustomerID>ALFKI</CustomerID>
    <
    CompanyName>Alfreds Futterkiste</CompanyName>
    <
    ContactName>Maria Anders</ContactName>
    <
    ContactTitle>Sales Representative</ContactTitle>
    <
    Address>Obere Str. 57</Address>
    <
    City>Berlin</City>
    <
    PostalCode>12209</PostalCode>
    <
    Country>Germany</Country>
    <
    Phone>030-0074321</Phone>
    <
    Fax>030-0076545</Fax>
    </customer>
    <customer>
    <
    CustomerID>ANATR</CustomerID>
    <
    CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <
    ContactName>Ana Trujillo</ContactName>
    <
    ContactTitle>Owner</ContactTitle>
    <
    Address>Avda. de la Constituci¢n 2222</Address>
    <
    City>M‚xico D.F.</City>
    <
    PostalCode>05021</PostalCode>
    <
    Country>Mexico</Country>
    <
    Phone>(5) 555-4729</Phone>
    <
    Fax>(5) 555-3745</Fax>
    </customer>
    <customer>
    <CustomerID>ANTON</CustomerID>
    <
    CompanyName>Antonio Moreno Taquer¡a</CompanyName>
    <
    ContactName>Antonio Moreno</ContactName>
    <
    ContactTitle>Owner</ContactTitle>
    <
    Address>Mataderos 2312</Address>
    <
    City>M‚xico D.F.</City>
    <
    PostalCode>05023</PostalCode>
    <
    Country>Mexico</Country>
    <
    Phone>(5) 555-3932</Phone>
    </customer>

    <CustomerName>

    <companyname>Blauer See Delikatessen</companyname>

    <companyname>Blondesddsl pŠre et fils</companyname>

    <companyname>B¢lido Comidas preparadas</companyname>

    <companyname>Bon app'</companyname>

    </CustomerName>

     


    Thanks

  • Re: Generating XML file in C# from SQL Database

    10-23-2008, 11:22 PM

    Zafar its very simple dont worry,

    first you fill your records into the dataset from there you can write the data into XML file before that you just create a xml file

    Ref: http://www.devasp.net/net/articles/display/219.html

    http://aspnet.4guysfromrolla.com/articles/052902-1.aspx

    if you want to create a xml file also dynamically then use this

    Ref: http://forums.asp.net/t/1193000.aspx

    http://www.tanguay.info/webold/codeExample.php5?id=426

    Any doubts please feel free to ask me.

    If this post is answer of your question then don't forgot to Click Mark As Answer.

    Thanks & Regards,
    J.Jeyaseelan
  • Re: Generating XML file in C# from SQL Database

    10-24-2008, 9:57 AM
    • Member
      4 point Member
    • zafarjaffary
    • Member since 10-19-2008, 4:21 PM
    • Posts 42

    Here is my code. My question is can i use this if want to pass parameter from method? Like i want to run this GetEmployees(String Country) then it run my following code and generate the xml file. It is creating the xml file but can i formate the xml file. And last i want to add another sql statement in the String Select * from employees where city ='London' it will show these result in the same xml file at bottom.

    Thanks 

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Xml;
    using System.IO;

    class ExecuteXmlReader
    {
    public static void Main()
    {
    String sConnection = "server=zjaffary\\SQLExpress;Integrated Security=SSPI;database=northwind";
    SqlConnection mySqlConnection = new SqlConnection(sConnection);
    mySqlConnection.Open();

    // Get the same data through the provider.

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("SELECT FirstName,LastName,Title,City from employees", sConnection);
    DataSet myDataSet2 = new DataSet();myDataSet2.DataSetName = "Region";
    mySqlDataAdapter.Fill(myDataSet2);

     

    // Write data to files: data1.xml and data2.xml for comparison.

     

    myDataSet2.WriteXml(
    "c:/temp/employees.xml");
    mySqlConnection.Close();
    }

    }

  • Re: Generating XML file in C# from SQL Database

    07-04-2009, 3:27 AM
    • Member
      16 point Member
    • vohrasohilhis
    • Member since 03-18-2009, 6:36 AM
    • Ahmedabad (Gujarat - India)
    • Posts 8

    Thanx dear....

    I was looking for such type of code...

    Sohil Vohra
    System Analyst & Developer
    Stalwart-India Pvt Ltd
    Ahmedabad (Gujarat - India)

Page 1 of 1 (4 items)