How to populate a DataSet schema with data from and XML string?

G

Guest

I have successfully loaded a DataSet object with a XML schema (XSD).

Now I wish to populate the tables that was created in the DataSet.
I have an XML file/string that contain all the needed data in the same
format as the XSD (the XML file/string was created using this same schema).
The XML file/string may contain data for a single table or for several
tables at once.

The question is:

How do populate a table in the DataSet using the data from an XML
file/string in the most easiest way?
When I say easiest I mean without parsing the XML node by node and
converting the node value to the specific row member value in the table in
the DataSet.
It sound very reasonable to expect for such a function that takes an XML
data from an XML string/file and populate a schema using it.
 
G

Guest

Hi Sharon,

Welcome to MSDN newsgroup.
As for the populating a DataSet's data with a existing Xml file which
contains the serilized table datas, if the xml file's content is exactly
match to the DataSet's schema or is direclty generated through
DataSet.WriteXml method, we can just use DataSet.ReadXml method to read the
data back to dataset instance from a xml file(or xml string, xml file
Stream....). Also the DataSet.ReadXmlSchema method can also help to load xml
schema from external file or stream. Here is their msdn reference:

#DataSet.ReadXml Method
http://msdn.microsoft.com/library/e...emdatadatasetclassreadxmltopic.asp?frame=true

#DataSet.ReadXmlSchema Method
http://msdn.microsoft.com/library/e...DataSetClassReadXmlSchemaTopic.asp?frame=true

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no rights.)
 
S

Steven Cheng[MSFT]

Hi Sharon,

Thanks for your response.
So based on your further description, my understanding on your problem is
that:

You have create a DataSet instance and populate its schema structure
through a schema. And now you may have some other strings(more than one),
and each string may contain data of of some certain Tables in the DataSet's
structure. You'd like to write a function which can help return DataRow of
Table from those strings and add into the Dataset, yes?

If so, I think we can consider the following means;

If the xml data in the string is exactly conform to the schema, we can
create another instance of the DataSet (which has populated its structure
with the same schema). After that, you can use this DataSet instance to
import Table data from those xml strings. We can use the
System.IO.StringReader to construct a reader from string, and use
DataSet.ReadXml
to populate data from that string. After that , you can try using
DataSet.Merge to merge data between this helper dataset and your original
dataset. Or you can also manually navigate into certain DataTable and use
DataTable.ImportRow to copy Row between datatable. Currently this is the
most considerable means I've found.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| Thread-Topic: How to populate a DataSet schema with data from and XML
string
| thread-index: AcWuwR64K2xiQkONSf2Ev+JwhaOfSw==
| X-WBNR-Posting-Host: 199.203.93.141
| From: =?Utf-8?B?U2hhcm9u?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: How to populate a DataSet schema with data from and XML
string
| Date: Wed, 31 Aug 2005 23:48:11 -0700
| Lines: 34
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:119645
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Steven,
|
| The ReadXml is not in my case.
| Allow me to clarify the problem.
|
| I have a DataSet loaded with an xml schema. So now the dataset contains
some
| tables and relations.
| I need to supply a function that receives an xml string containing one or
| more entries in some table like:
| <Defect>
| <ID>0</ID>
| <Type>hole</Type>
| <Sevirity>1</Sevirity>
| <XCoordinate>20</XCoordinate>
| <YCoordinate>300</YCoordinate>
| <Deleted>false</Deleted>
| <DefectUI>
| <Shape>Circle</Shape>
| <RedColor>0</RedColor>
| <GreenColor>100</GreenColor>
| <BlueColor>255</BlueColor>
| </DefectUI>
| </Defect>
|
| The Defect table may contain more sub tables and the also the Defect
table
| may be a sub table of another table.
|
| Is there a straight forward way to fill the dataset with the values given
by
| the string xml like the shown above?
|
|
| --------
| Thanks
| Sharon
|
 
S

Steven Cheng[MSFT]

Thanks for your response Sharon,

Yes, building a DataSet will have much performance cost so my original
thought is also as you mentioned using a SINGLETON pattern to host a single
Helper dataset instance to do the work. Anyway, if you really feel high
peformance concerns on this, I think your current choice on manually loop
the XML doc will work better.

Thanks & Regards,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Thread-Topic: How to populate a DataSet schema with data from and XML
string
| thread-index: AcWxIOyCr2ZF77nqTk2KwNpGi0XICw==
| X-WBNR-Posting-Host: 199.203.93.141
| From: =?Utf-8?B?U2hhcm9u?= <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: How to populate a DataSet schema with data from and XML
string
| Date: Sun, 4 Sep 2005 00:19:01 -0700
| Lines: 20
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:120158
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Steve,
|
| Your suggestion will work, but the creation of a DataSet each time the
API
| function is called us very expensive. You can read about it at
| http://www.codeproject.com/csharp/TypedDSProxy.asp
|
| I can hold only one more DataSet for this purpose but I still think it’
s an
| expensive solution.
|
| I simply thought that a function like that, that just take an XML
containing
| the table/row values and put them in the already built table/row exist.
| I guess I was mistaken.
|
| So I made one on my own by iterating on the XML nodes and setting each
value
| in the DataSet accordingly.
|
|
| ---------
| Thanks
| Sharon
|
 

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

Similar Threads


Top