.NETGURU
Urgent Help Needed Converting from XmlNode to Dataset
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Mae (VIP)
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
Reply to this message...
 
    
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)
"Mae" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Mae (VIP)
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

"Hermit Dave" wrote:

[Original message clipped]

Reply to this message...
 
    
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)
"Mae" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.ComponentModel.Container
System.ComponentModel.ISupportInitialize
System.Data.DataSet
System.Drawing.Point
System.Drawing.Size
System.Drawing.SystemColors
System.EventArgs
System.EventHandler
System.Windows.Forms.Application
System.Windows.Forms.Button
System.Windows.Forms.DataGrid
System.Windows.Forms.Form
System.Windows.Forms.MessageBox
System.Xml.XmlDataDocument
System.Xml.XmlNode




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification