XML Export-Import

G

Ghost

Hello.

What is the optimal way to manualy import/export data from/to XML file
to/form my DataSet?

What I wnat:
1. To add some data (records) from XML file to may dataset.
2.. To add some data (records) from dataset to may XML file.

For example:

Table structure:
ID int Primary Key
FirstName VarChar (15)
LastName VarChar (15)
Age int

XML file structure:
<Pesrons>
<ID> </ID>
<FirstName> </FirstName>
<LastName> </LastName>
<Age> </Age>
</Pesrons>

If it's poseble show me little example or some demo please.
 
M

Munir Husseini

Hi David,

I'd get the dataset's XML with

string xml = dataset1.GetXml();
and transform that xml into my desired output using XSLT. This way you can
always change your putput without having to change your program's code.
Beside that, this way you can easily ensures that your XML is valid.



To use XSLT youe have to do something like this:



XPathNavigator xpath = doc.CreateNavigator(); //doc is the XmlDocument you
got from dataset1.GetXml()

XmlResolver xmlResolver = null;

MemoryStream ms = new MemoryStream();

XsltArgumentList args = new XsltArgumentList();

NameTable nt = new NameTable();

XmlNamespaceManager nsm = new XmlNamespaceManager(nt);

XmlValidatingReader xmlR = new XmlValidatingReader([your XSLT code here],
XmlNodeType.Document, new XmlParserContext(null, nsm, null,XmlSpace.None));


XslTransform xslt = new XslTransform();

xslt.Load(xmlR, null, null);

xslt.Transform(xpath, args, ms, xmlResolver);



After that ms carries the output XML you wanted to get. You could save it
into a file or whatever.

To load XML into a dataset you have to do the same, but the input is the XML
you can load from wherever and after transforming it using XSLT you can use

dataset1.ReadXml(ms);



This is the way I'd preferr as the most professional. Hope this helps.

For a brief intruduction into XSLT, try
http://www.xml.com/pub/a/2000/08/holman/.



Regards,

Munir Husseini

iCOMcept GmbH

www.icomcept.com
 
G

Ghost

Oh, it's great!
but can you provide me more simple exanple without XSLT?

Munir Husseini said:
Hi David,

I'd get the dataset's XML with

string xml = dataset1.GetXml();
and transform that xml into my desired output using XSLT. This way you can
always change your putput without having to change your program's code.
Beside that, this way you can easily ensures that your XML is valid.



To use XSLT youe have to do something like this:



XPathNavigator xpath = doc.CreateNavigator(); //doc is the XmlDocument you
got from dataset1.GetXml()

XmlResolver xmlResolver = null;

MemoryStream ms = new MemoryStream();

XsltArgumentList args = new XsltArgumentList();

NameTable nt = new NameTable();

XmlNamespaceManager nsm = new XmlNamespaceManager(nt);

XmlValidatingReader xmlR = new XmlValidatingReader([your XSLT code here],
XmlNodeType.Document, new XmlParserContext(null, nsm, null,XmlSpace.None));


XslTransform xslt = new XslTransform();

xslt.Load(xmlR, null, null);

xslt.Transform(xpath, args, ms, xmlResolver);



After that ms carries the output XML you wanted to get. You could save it
into a file or whatever.

To load XML into a dataset you have to do the same, but the input is the XML
you can load from wherever and after transforming it using XSLT you can use

dataset1.ReadXml(ms);



This is the way I'd preferr as the most professional. Hope this helps.

For a brief intruduction into XSLT, try
http://www.xml.com/pub/a/2000/08/holman/.



Regards,

Munir Husseini

iCOMcept GmbH

www.icomcept.com





Ghost said:
Hello.

What is the optimal way to manualy import/export data from/to XML file
to/form my DataSet?

What I wnat:
1. To add some data (records) from XML file to may dataset.
2.. To add some data (records) from dataset to may XML file.

For example:

Table structure:
ID int Primary Key
FirstName VarChar (15)
LastName VarChar (15)
Age int

XML file structure:
<Pesrons>
<ID> </ID>
<FirstName> </FirstName>
<LastName> </LastName>
<Age> </Age>
</Pesrons>

If it's poseble show me little example or some demo please.
 

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

Top