Solved

Create customised xml from dataset c#

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

20 Experts available now in Live!

Get 1:1 Help Now