Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending XML data to dataset 1

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I'm currently reading data from an XML file using XPathNavigator and XPathNodeIterator to select the subset of data that I want. I've never using the XPath objects before so I'm not sure how to get this data into a dataset.

XPathDocument doc = new XPathDocument(@"C:\inetpub\ XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = nav.Select("/test/translation/engword[preceding-sibling::fraword = '" + strFraWord + "']");

Can anybody help me out with this? Any assistance would be really appreciated.


Cheers,

lfc77
 
To load xml file into a DataSet:
Code:
DataSet newDataSet = new DataSet("My_DataSet");
newDataSet.ReadXml(xmlFilename);
Is I can see from your post, the above code should be part of a web method in a web service and you obtain the DataSet object like:
Code:
MyWebService oSrv = new MyWebService ();
DataSet ds = MyWebService.LoadxmlFile((@"C:\inetpub\[URL unfurl="true"]wwwroot\test\test.xml");[/URL]
where LoadxmlFile is the WebMethod in the MyWebService class.
-obislavu-
 
Obislavu,

Your example loads a complete XML file into a dataset. What I need to do is select particular records in the XML file and then load those into the dataset (that's why I was using the XPath stuff, but maybe that's not the right way to do it?)


lfc77
 
I do not see any problem if you use XmlDocument class to load the xml file into an XmlDocument, call SelectNodes() on the root to get a list of XmlNode objects, iterate through that list and import selected node into another XmlDocument object.
Next, call Save() on this 2nd XmlDocument object to create a file with the selected nodes only.
Next, load it directly as I said before into a DataSet.
Code:
Use XmlDocument to load/import selected nodes
//Create two XmlDocument.
    XmlDocument doc1 = new XmlDocument();
    XmlDocument doc2 = new XmlDocument();
    doc1.Load("myfile.xls");
      //Import the last book node from doc2 into the original document.
    XmlNode newBook = doc.ImportNode(doc2.DocumentElement.LastChild, true);
    doc.DocumentElement.AppendChild(newBook);



// Select desired nodes 

 XmlNodeList nodeList;
XmlElement root = doc1.DocumentElement;
nodeList = root1.SelectNodes("/??????, nsmgr);
foreach (XmlNode nd in nodeList)
{
   XmlNode importednode = doc2.ImportNode(nd, true);
   doc2.DocumentElement.AppendChild(importednode);
   
}
doc2.Save("myfile2.xls")
DataSet newDataSet = new DataSet("My_DataSet");
newDataSet.ReadXml("myfile2.xls");
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top