Solved

Export XML using C# as Attribute Rows

Posted on 2014-03-22
2
466 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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

13 Experts available now in Live!

Get 1:1 Help Now