Solved

Load part of XML file into XElement?

Posted on 2010-11-25
5
717 Views
Last Modified: 2012-05-10
Hi,

I have a folder of XML files and I need to get 2 node values from each of them to display in a datagrid on a web page.
Both of these values are near the top of the XML node tree, but the files themselves could be rather large, from 1Mb to 100Mb+,  and there could be 100+ files in the folder.
Is there any way of loading only the top part of the file into the XElement to save time?
My XML structure is below.
I need the ID and the Status.

Many Thanks
<Instruction>
  <SavePath>AA.XML</SavePath>
  <TimeStamp>24/11/2010 10:10:59</TimeStamp>
  <ID>123456</ID>
  <Date>26 July 2010</Date>
  <Time>16:44</Time>
  <Investigation>
    <Status>6</Status>
    <AllocatedTo>765</AllocatedTo>
  </Investigation>
  .....
  Lots more nodes
  .....
</Instruction>

Open in new window

0
Comment
Question by:janineo
  • 3
5 Comments
 
LVL 3

Expert Comment

by:kraiven
ID: 34213457
This might be what you want;

var xdoc = XDocument.Load(@"D:\Temp\Sample2.xml");

var xmlOut = new XDocument(
				from el in xdoc.Elements("Instruction").Elements("ID")
				from el2 in xdoc.Elements("Instruction").Elements("Investigation").Elements("Status")
				select new XElement("Extract", 
							new XElement("Id", (int)el),
							new XElement("Status", (int)el2)));

Open in new window


Sample2.xml contains your data as above.
Output is:
<Extract>
  <Id>123456</Id>
  <Status>6</Status>
</Extract>

Open in new window

0
 

Author Comment

by:janineo
ID: 34214178
Thanks, but it's not quite what I need.
Your post is still loading in the whole document to start with, I don't want to do that if possible.

I know how to get the data out from an XDocument or XElement, just not if it's possible to only load part of an XML file into the xElement.
0
 
LVL 7

Accepted Solution

by:
deadlyDev earned 500 total points
ID: 34217560
I'm not very familiar with it, but you could try using an xml reader....

Read until you have your required nodes....
Close the file stream
Manually append the end of the xml document (The end tag... ie </Instruction>)
Load the "sub" document into an xdocument
0
 

Author Comment

by:janineo
ID: 34218485
That sounds like it might work.
I'll take a look on Monday, thanks.
0
 

Author Comment

by:janineo
ID: 34290630
Ok, I got it to work using XmlReader and ReadToFollowing.

As I only needed 2 node values from near the top of the XML file this worked well for me:

Using xR As XmlReader = XmlReader.Create(ioF.FullName)
   xR.ReadToFollowing("AssignmentID")
   strID = xR.ReadString
   xR.ReadToFollowing("InvestigationStatus")
   strStatus = xR.ReadString
End Using

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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