Solved

Create customised xml from dataset c#

Posted on 2006-11-14
2
284 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 125 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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