Urgent Help Needed Converting from XmlNode to Dataset

G

Guest

Dear All,

I have a problem here, I'm using C# Webform calling a webservices. The
webservices return me a XMLnode, using this XMLnode I want to convert it to
dataset so I can bind to the datagrid, by extracting the
<CustomerData></CustomerData> block from the xmlnode.

Below is the sample of xmlnode return from webservices.

<?xml version="1.0" encoding="utf-8"?>
<OutputParams>
<CustomerData>
<CustomerID>1</CustomerID>
<CustomerName>TEST1</CustomerName>
<Gender>M </Gender>
<DateOfBirth>19800101</DateOfBirth>
</CustomerData>
<CustomerData>
<CustomerID>1</CustomerID>
<CustomerName>TEST2</CustomerName>
<Gender />
<DateOfBirth>19000101</DateOfBirth>
</CustomerData>
<CustomerData>
<CustomerID>3</CustomerID>
<CustomerName>TEST3</CustomerName>
<Gender />
<DateOfBirth>19000101</DateOfBirth>
</CustomerData>
<ReturnCode>0</ReturnCode>
<ErrorCode>0</ErrorCode>
<ErrorDescription />
</OutputParams>

How can it be done? Please provide some examples on this.
Thanks in advance.


Cheers,
mae
 
H

Hermit Dave

you need something like this

XmlNode xn = new XmlNode();

// the above represents you xml node... replace all instances with your
node.

DataSet dsXML = new DataSet();

XmlDataDocument xdd = new XmlDataDocument(dsXML);

xdd.LoadXml(xn.OuterXml);

// this should populate the dataset

if you have any problems.. message back.. i will try and give you a complete
working example


--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
G

Guest

Hi Hermit,

I'm newbie to XML, I'm not sure how it works.
Can you provide me a complete working examples, that shows how to the
required <CustomerData></CustomerData> block bind to datagrid ?

Thanks in advance.

Cheers,
Mae
 
H

Hermit Dave

i created a xmlfile from your node list but as i said you can directly use
the OuterXML
i will copy contents off each file and add those here. Hope this helps

Customers.xml
---------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<OutputParams>
<CustomerData>
<CustomerID>1</CustomerID>
<CustomerName>TEST1</CustomerName>
<Gender>M</Gender>
<DateOfBirth>19800101</DateOfBirth>
</CustomerData>
<CustomerData>
<CustomerID>1</CustomerID>
<CustomerName>TEST2</CustomerName>
<Gender />
<DateOfBirth>19000101</DateOfBirth>
</CustomerData>
<CustomerData>
<CustomerID>3</CustomerID>
<CustomerName>TEST3</CustomerName>
<Gender />
<DateOfBirth>19000101</DateOfBirth>
</CustomerData>
<ReturnCode>0</ReturnCode>
<ErrorCode>0</ErrorCode>
<ErrorDescription />
</OutputParams>
---------------------------------------------------
Customers.xsd (used xsd to load the schema - Cause you do not want the
Return Code ErrorCode & Description in the dataset)
---------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="OutputParams" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="OutputParams">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="CustomerData">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomerID" type="xs:int"></xs:element>
<xs:element name="CustomerName" type="xs:string"></xs:element>
<xs:element name="Gender" type="xs:string"></xs:element>
<xs:element name="DateOfBirth" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
---------------------------------------------------
StepByStep2_9.cs
---------------------------------------------------
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace StepByStep
{
/// <summary>
/// Summary description for StepByStep2_9.
/// </summary>
public class StepByStep2_9 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dgXML;
private System.Windows.Forms.Button btnLoadXML;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public StepByStep2_9()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgXML = new System.Windows.Forms.DataGrid();
this.btnLoadXML = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dgXML)).BeginInit();
this.SuspendLayout();
//
// dgXML
//
this.dgXML.DataMember = "";
this.dgXML.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgXML.Location = new System.Drawing.Point(16, 64);
this.dgXML.Name = "dgXML";
this.dgXML.Size = new System.Drawing.Size(568, 200);
this.dgXML.TabIndex = 3;
//
// btnLoadXML
//
this.btnLoadXML.Location = new System.Drawing.Point(16, 24);
this.btnLoadXML.Name = "btnLoadXML";
this.btnLoadXML.TabIndex = 2;
this.btnLoadXML.Text = "Load XML";
this.btnLoadXML.Click += new System.EventHandler(this.btnLoadXML_Click);
//
// StepByStep2_9
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(600, 285);
this.Controls.Add(this.dgXML);
this.Controls.Add(this.btnLoadXML);
this.Name = "StepByStep2_9";
this.Text = "StepByStep2_9";
this.Load += new System.EventHandler(this.StepByStep2_9_Load);
((System.ComponentModel.ISupportInitialize)(this.dgXML)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new StepByStep2_9());
}

private void btnLoadXML_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXmlSchema(@"..\..\Customers.xsd");

// create matching xml data document
XmlDataDocument xdd = new XmlDataDocument(ds);
try
{
xdd.Load(@"..\..\Customers.xml");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error Loading XML");
}
this.dgXML.DataSource = ds;
this.dgXML.DataMember = "CustomerData";
}
}
}

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top