Solved

Load part of XML file into XElement?

Posted on 2010-11-25
5
721 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
[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
  • 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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