0

On exporting all tables from a sql database to xml the table name nodes are named table,table1,table2 etc..How to get the table name exactly as in database on exporting to xml file?

7
  • There are some questions: Which RDBMS (vendor and version)? What are you trying to export (structure, data, both)? One big file or many small files? What does "export" mean exactly (just XML files somewhere)? If you want to do this from your C#-application: How are you connected (EF, typed/untyped DataSet, other...)? If you want to do this from your RDBMS: You have to think about rights, server restrictions, reachable paths... Commented Feb 24, 2016 at 7:50
  • I am working in EF.I am fetching all tables in my databse to xml using dataset. Commented Feb 24, 2016 at 9:45
  • There is an answer already which tells you, that the DataSet has built in support for exporting to XML (data, schema or both). What else do you need? Commented Feb 24, 2016 at 9:47
  • If you want to make XML lines visible to have to mark them as code like <some xml="marked"/> Commented Feb 24, 2016 at 10:11
  • please use the edit option to put more details into your original question Commented Feb 24, 2016 at 10:11

3 Answers 3

1

Not sure how exported, if you exported with DataSet, you must not be having table names for each table in DataSet, make sure that tables in your dataset has names.

        DataSet ds = new DataSet();
        foreach (DataTable d in ds.Tables)
        {
            d.TableName = "TableName";
        }
        ds.WriteXml("filepath");
        //Or
        DataTable dt = new DataTable("TableName");
        dt.WriteXml("filepath");
Sign up to request clarification or add additional context in comments.

Comments

0

if you using asp you need xdocument,xelement,xdocumenttype,xattribute,XDeclaration

 XDocument XMLDocument=new XDocument();
    XDeclaration Declaration=new XDeclaration("1.0", "utf-8", "yes");
    XDocumentType doctype = new XDocumentType("filename", "", "",null);
    XMLDocument.AddFirst(doctype);
    XMLDocument.Declaration=Declaration;

Comments

0

welcome to Stackoverflow.

I was also facing the same issue in one of my projects. As an alternative, I wrote the query to retrieve an XML in my column and write the data of retrieved from this column into my file.

The query example is as follows;

SELECT (
  SELECT TOP 1 NULL as N,
    (SELECT Employee.EmployeeId as Id, Employee.FirstName as Name,
      (SELECT TOP 1 NULL as N,
        (SELECT Customer.CustomerID as Id, CustomerName as Name
         FROM EmployeesCustomers INNER JOIN Customers Customer
         ON EmployeesCustomers.CustomerID = Customer.CustomerID
         WHERE EmployeesCustomers.EmployeeId = Employee.EmployeeId
         ORDER BY Customer.CustomerID
         FOR XML AUTO, TYPE)
       FROM Customers
       FOR XML AUTO, TYPE)
     FROM EmployeesCustomers INNER JOIN Employees Employee
     ON EmployeesCustomers.EmployeeID = Employee.EmployeeID
     GROUP BY Employee.EmployeeId, Employee.FirstName
     ORDER BY Employee.EmployeeId
     FOR XML AUTO, TYPE)
  FROM Employees
  FOR XML AUTO, ROOT('Company')
) AS COL_XML

The complete details is available in the following link (even though the link is for SSIS, the concept works in all cases) Click here

If you have a very complex query and cannot get the output as need, you can go ahead with the approach suggested by Siva Kumar Siddam to have the names of tables provided to your dataset.

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.