Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was writing an application for my Project in which XML documents have to be parsed using C#. So it was something like that:

1st file:

XML
<name> Name of Student </name>
              <class> ClassofStudent </class>
and so on..

2nd file :

<monday>
XML
<period1>C++</period1>
                       <period2>Maths</period2>
and so onnn

and their are 5 files more like them.The thing is that only one of the file contains a huge number of elements ( its a kind of attendance record for whole month) while others are short.

I was just googling everything and found out there are different methods to parse XML in C#, but some face issues like high memory consumption or relative less speed.

The matter that i am unable to make decision is which of the above should i use (XMLDocument or XDocument or XmlReader or LINQ to Xml )

Or as my purpose will not be that much high , so it will not matter me what i choose to use?
I prefer Styling== performance , as i have to later style them also(GUI).And which one will be easier to implement too.
Thankyou
Posted

1 solution

It all depends on a number of different factors. The easiest would be XMLDocument or XDocument (depending on your tasks, also, I would doubt that XDocument without LINQ to XML would pay off, so consider using LINQ with that), but XmlReader/XmlWriter can get you top performance. Please see my short review of those approaches:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
Comments
Abhinav Gauniyal 24-Sep-13 15:16pm    
Sir , Can we use all of the above methods to read that XML file from some URL instead of local storage?
Sergey Alexandrovich Kryukov 24-Sep-13 16:41pm    
Absolutely. You can use the class System.Net.HttpWebRequest. The instance is obtained through the factory method (static) System.Net.HttpRequest.Create:
http://msdn.microsoft.com/en-us/library/0aa3d588.aspx (the actual runtime class is defined by the URI scheme: it this is http://, you will get System.Net.HttpWebRequest and can bravely cast to this type). You can even add authentication and more.

The code sample is here: at the end of this MSDN page.

You create an HTTP request and obtain the instance of HttpWebResponse, it will give your the response stream:
Stream dataStream = response.GetResponseStream();
then you read XML from this stream as you would from any other stream. So, you can do it on the fly, without saving raw data in the file.

I hope you know enough now, please try. If you face any problem you cannot solve, your questions will be welcome.

—SA
Abhinav Gauniyal 25-Sep-13 9:33am    
Perfect , Thanks :)
Sergey Alexandrovich Kryukov 25-Sep-13 10:16am    
You are very welcome.
Good luck, call again.
—SA

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