Solved

Load part of XML file into XElement?

Posted on 2010-11-25
5
714 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now