Solved

Export XML using C# as Attribute Rows

Posted on 2014-03-22
2
467 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
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Peformance + mulitple query plans 9 47
T-SQL:  Negative Numbering in CTE Is Not Working 2 29
SQL Query stumper 3 36
Sql Query with datetime 3 10
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
Viewers will learn how the fundamental information of how to create a table.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now