?
Solved

Load Partial XML Into Dataset

Posted on 2010-01-09
5
Medium Priority
?
1,653 Views
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:

<vehicles>
<cars>
<car>
<brand>
Toyota
</brand>
</car>
</cars>
<trucks>
<truck>
<brand>
Ford
</brand>
</truck>
</trucks>
</vehicles>

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

Please advise.
0
Comment
Question by:GY1680
[X]
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
5 Comments
 
LVL 63

Accepted Solution

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

This should do what you need.

Fernando
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)
{
    outDoc.Add(ele);
}
// 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
ds.ReadXml(ms);
// Display the info in a data grid view
dataGridView1.DataSource = ds.Tables[0];

Open in new window

0
 
LVL 5

Expert Comment

by:chinawal
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

0
 
LVL 5

Expert Comment

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

Author Closing Comment

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

Expert Comment

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

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