Click here to Skip to main content
16,021,004 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to fetch the node values from an Xml in the fastest manner ?
I want to fetch nodes in the most fast manner. I have tried using Xml Node List and then fetch the XmlNode from the same and loop using foreach and validate. It is time consuming.
Posted

Most likely, the fastest will be using the class System.Xml.XmlReader:
https://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110%29.aspx[^]

Only if your XML is extremely small, the faster method could be some ad-hoc string search in it, but I would recommend to avoid such things by all means, by pretty obvious reasons.

—SA
 
Share this answer
 
Comments
Maciej Los 22-Oct-15 1:53am    
5ed!
Sergey Alexandrovich Kryukov 22-Oct-15 2:47am    
Thank you, Maciej.
—SA
Matt T Heffron 22-Oct-15 12:37pm    
+5
Sergey Alexandrovich Kryukov 22-Oct-15 14:23pm    
Thank you, Matt.
—SA
As Sergey Aleksandrovich Kryukov[^] mentioned it depends on file size. For small portion of data the difference measured in time of reading xml file is unnoticeable, even if you use Linq to xml[^].

Imagine, an xml structure looks like:
Root
--Level1
  |--Level2
    |--Level3
      |--Level4
        |--Level5
          |--Level6
            |--Level7
              |--Level8

You want to get nodes (data) from node at Level8, so you can achieve that using query like this:
C#
XDocument xdoc = XDocument.Load("fullfilename.xml");
var result = xdoc.Descendants("Level8")
    .Descendants()
    .Select(a=>a);


For further information, please see:
LINQ to XML[^]
LINQ to XML Overview[^]
Basic Queries (LINQ to XML)[^]
 
Share this answer
 
v2
Comments
Matt T Heffron 22-Oct-15 12:38pm    
+5
Maciej Los 22-Oct-15 12:41pm    
Thank you, Matt
Sergey Alexandrovich Kryukov 22-Oct-15 14:24pm    
Agree, a 5.
—SA
Maciej Los 22-Oct-15 14:33pm    
Thank you, Sergey.
node.SelectNodes("some XPath here");
This is fastest way to select node without looping concept.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900