Solved

Export XML using C# as Attribute Rows

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

770 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