?
Solved

Create customised xml from dataset c#

Posted on 2006-11-14
2
Medium Priority
?
288 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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