Load Partial XML Into Dataset

Posted on 2010-01-09
Medium Priority
Last Modified: 2013-11-11
I'm trying to load xml from a file into a dataset, and I can do it using LINQ and XDocument.ReadXML(file) - but I'd like to only fill the dataset with a portion of the xml file's contents, rather than the entire file.  I'm wondering how best to give it a start tag or otherwise modify the dataset to only include the xml I want.

For example:


In this example - I'd only like to populate my dataset with the Trucks values - and disregards the Cars values.

Please advise.
Question by:GY1680
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 63

Accepted Solution

Fernando Soto earned 2000 total points
ID: 26275815
Hi GY168;

This should do what you need.

using System.Xml;
using System.Xml.Linq;
using System.IO;

// Load XML document from disk
XDocument doc = XDocument.Load("XmlData.xml");
// The new document to hole the modified XML
XDocument outDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
// Linq query to get all the trucks elements
var results = from ele in doc.Descendants("trucks")
              select ele;
// Add the elements found in the result set.
foreach (XElement ele in results)
// Create a byte array from the outDoc document
byte[] xmlBytes = new byte[outDoc.ToString().Length];
xmlBytes = Encoding.UTF8.GetBytes(outDoc.ToString());
// Read it into a memory stream to use with data set
MemoryStream ms = new MemoryStream(xmlBytes);
// Create the new data set
DataSet ds = new DataSet();
// Read the XML into the data set
// Display the info in a data grid view
dataGridView1.DataSource = ds.Tables[0];

Open in new window


Expert Comment

ID: 26275900
Try this

select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate
from tblSalesHeader h
      left join
            (select top 1 a.*
             from Audit a
             inner join tblSalesHeader h
             on a.RecordKey = h.SalesHeaderID
             order by a.updatedate desc) x
 on x.recordkey = h.salesheaderid


Expert Comment

ID: 26275903
sorry.. too many windows open.. wrong answer to wrong question..

Author Closing Comment

ID: 31675066
Worked perfectly.  Thank you very much for your help.
LVL 63

Expert Comment

by:Fernando Soto
ID: 26280386
Not a problem, glad I was able to help.  ;=)

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question