Solved

Export XML using C# as Attribute Rows

Posted on 2014-03-22
2
480 Views
Last Modified: 2014-03-22
I need my exports to look like this;
<?xml version="1.0" encoding="utf-8" ?>
<Items>
    <Item Name="Afghanistan" ID="296" />
<Items>

Open in new window

and NOT this (as they are now)
<?xml version="1.0" standalone="yes"?>
<Items>
  <Item>
    <ID>296</ID>
    <Name>Afghanistan</Name>
  </Item>

Open in new window

My export Code is this;
            
var adapter4 =
new SqlDataAdapter("SELECT [Name],[ID],[PID1] FROM dbo.Countries where PID1 = 14  ORDER BY Name ASC", connection);
var dt4 = new DataTable("Item");
adapter4.Fill(dt4);
DataSet ds4 = new DataSet("Items");
ds4.Tables.Add(dt4);
ds4.WriteXml("C:\\inetpub\\wwwroot\\XR8\\App_Data\\XML\\Countries.xml", System.Data.XmlWriteMode.IgnoreSchema);

Open in new window

What am I missing? Please advise?
0
Comment
Question by:XGIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 500 total points
ID: 39947292
var adapter4 =
new SqlDataAdapter("SELECT [Name],[ID],[PID1] FROM dbo.Countries where PID1 = 14  ORDER BY Name ASC", connection);
var dt4 = new DataTable("Item");
adapter4.Fill(dt4);
DataSet ds4 = new DataSet("Items");
foreach (DataColumn col in dt4 .Columns)
            {
                if (col.ColumnName == "ID" || col.ColumnName == "Name")
                {
                    col.ColumnMapping = MappingType.Attribute;
                }
                else
                {            
                    col.ColumnMapping = MappingType.Hidden; //Ignoring PID1
                }
            }

ds4.Tables.Add(dt4);
ds4.WriteXml("C:\\inetpub\\wwwroot\\XR8\\App_Data\\XML\\Countries.xml", System.Data.XmlWriteMode.IgnoreSchema);
0
 
LVL 7

Author Closing Comment

by:XGIS
ID: 39947381
Hello jayakrishnabh...
I did try a for each but obviously not the right way. Excellent Job.. it data-binded immediately.
Thankyou
Aaron
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question