Solved

Load part of XML file into XElement?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 32
Change to file doesn't show up 16 52
Where does legacy ASP.NET initialize language? 2 25
C# Service FileSystemwatcher isse 9 23
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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. 
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 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