Solved

Load part of XML file into XElement?

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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