[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Create customised xml from dataset c#

Posted on 2006-11-14
2
Medium Priority
?
290 Views
Last Modified: 2008-03-03
Hi,
I have a select query like this

Select value,key,id from table;
Which returns the result like this
value key id
a 101 1
b 102 1
a 103 2
a 104 2

actully the resultset is in dataset i need to manipulate the dataset and generate the xml
like this
How to generate the xml please help

<AvailableFields>
<Table Name="table1">
<Type Name="a">
<Fields Key="101" id="1"/>
<Fields Key="103" id="2" />
<Fields Key="104" id="2" />
</Type>
<Type Name="b">
<Fields Key="103" id="1"/>
</Type>
</Table>
</AvailableFields>

Regards,
nanban.
0
Comment
Question by:nanban
[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 8

Expert Comment

by:sabeesh
ID: 17937401
DataTable dtXmlLink = new DataTable("XmlLink");
dtXmlLink.Columns.Add("Key",typeof(int));
dtXmlLink.Columns.Add("Id",typeof(string));
--Loop---
DataRow drXmlLink = dtXmlLink.NewRow();
drXmlLink["Key"]= "";// loop thourgh and add values in to datatable
drXmlLink["Id"]= "";
dtXmlLink.Rows.Add(drXmlLink);
---end loop---

                  {
                        DataSet ds = new DataSet();
                        ds.Tables.Add(dtXmlLink );
                        MemoryStream memStream = new MemoryStream();
                        StreamReader streamReader = new StreamReader(memStream);
                        ds.WriteXml(memStream, XmlWriteMode.IgnoreSchema);
                        memStream.Seek(0, SeekOrigin.Begin);    
                        xmlString = streamReader.ReadToEnd();    
                  }
                        
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 375 total points
ID: 17937497
                 XmlDocument doc = new XmlDocument();
                  doc.LoadXml("<AvailableFields><Table Name=\"table1\"></Table></AvailableFields>");
                  XmlNode tableNode = doc.DocumentElement.ChildNodes[0];

                  DataSet data = new DataSet();
                  DataTable table = new DataTable("table1");
                  data.Tables.Add(table);
                  table.Columns.Add(new DataColumn("value", typeof(string)));
                  table.Columns.Add(new DataColumn("key", typeof(int)));
                  table.Columns.Add(new DataColumn("id", typeof(int)));

                  DataRow newRow = table.NewRow();
                  newRow.ItemArray = new object[]{"a", 101, 1};
                  table.Rows.Add(newRow);

                  newRow = table.NewRow();
                  newRow.ItemArray = new object[]{"b", 102, 1};
                  table.Rows.Add(newRow);

                  newRow = table.NewRow();
                  newRow.ItemArray = new object[]{"a", 103, 2};
                  table.Rows.Add(newRow);

                  newRow = table.NewRow();
                  newRow.ItemArray = new object[]{"a", 104, 2};
                  table.Rows.Add(newRow);
            
                  foreach(DataRow row in data.Tables[0].Rows){
                        string type = row["value"].ToString();
                        string key = row["key"].ToString();
                        string id = row["id"].ToString();
                        
                        XmlNode typeNode = tableNode.SelectSingleNode("Type[@Name = '" + type + "']");
                        if (typeNode == null){
                              typeNode = doc.CreateElement("Type");
                              ((XmlElement)typeNode).SetAttribute("Name", type);
                              tableNode.AppendChild(typeNode);
                              
                        }
                        XmlElement newField = doc.CreateElement("Fields");
                        newField.SetAttribute("Key", key);
                        newField.SetAttribute("id", id);
                        typeNode.AppendChild(newField);
                  }
                  
                  Console.WriteLine(doc.OuterXml);
                  Console.ReadLine();
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

650 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