Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.80/5 (2 votes)
I have a 2 GB xml file.
I can use the .Net's feature dataset.loadxml() method to read this file. But it's very much slow.

My question : is there any other way to read xml file and load it to a dataset. I need to load this in a dataset

Please help!
Posted

For large xml files, loading the whole xml will be slow and inefficient which results in poor performance. Here is an article in CP itself which addressed this issue:

http://www.codeproject.com/Articles/156982/How-to-Open-Large-XML-files-without-Loading-the-XM.aspx[^]
 
Share this answer
 
Comments
Angsuman Chakraborty 6-Mar-11 3:18am    
myID = RewardXmlDoc.SelectSingleNode("Reward/myID").InnerText;

Hi there Tarun,
Thanks for this good article. It would really help the developers world.

I have an inquire about the .selectsinglenode method.

I have a dataset which writes an xml file using Dataset.Writetoxml() in .Net 4.0;

But the problem is if a particular cell in data table has empty or null value, the writetoxml(); method skips that attribute.

Suppose i have a column in data table , lets call this client_address. some client may not provide their address.
If the client_address is empty, the Write to xml method doesnot write <client_address><client_address>.
This attribute is skipped.

So when i use
myclientAddress = RewardXmlDoc.SelectSingleNode("Reward/Client_Addess").InnerText; it gives me error, since this tag doesn't exists.

my question is how can i determine if this element exists in the xml file.
Please check following link

Reading Xml with XmlReader in C#[^]
 
Share this answer
 
As soon as you define your XML namespace used in the XML elements, you can easily import this - no problem.

You need to have your XML look something like this:

XML
<Catalog xmlns:dt="some-xml-namespace-here">
 <Rec>
   <ITEM dt:dt="string"/>
   <QTY dt:dt="string">1</QTY>
   <SUB dt:dt="string">1</SUB>
   <CATALOG dt:dt="string">ABC123</CATALOG>
  </Rec>
  .....
 </Catalog>

After I do this, your two lines of code work like a charm and the data gets imported, no problem (into 5 tables inside the Dataset.
 
Share this answer
 
v2
If you have the XML file like

XML
<?xml version="1.0" encoding="utf-8"?>
<AppConfigItems>
  <Keys>
    <KeyNames>USID</KeyNames>
    <KeyValues>184</KeyValues>
  </Keys>
  <Keys>
    <KeyNames>AdminEmail</KeyNames>
    <KeyValues>abcd@abcd.com</KeyValues>
  </Keys>
  <Keys>
    <KeyNames>PopServerName</KeyNames>
    <KeyValues>pop.gmail.com</KeyValues>
  </Keys>
  <Keys>
    <KeyNames>PortNumber</KeyNames>
    <KeyValues>995</KeyValues>
  </Keys>
</AppConfigItems>



You can go for

MSIL
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(ConfigurationManager.AppSettings["AppConfigXMLFileForJobPostByEmail"].ToString() + "AppConfigSettings.xml");
XmlNodeList list = myXmlDocument.SelectNodes("/AppConfigItems/Keys");
foreach (XmlNode stats in list)
{
     if (stats["KeyNames"].InnerText == "AdminEmail")
     {
          string adminemail = stats["KeyValues"].InnerText;//abcd@abcd.com
     }
     if (stats["KeyNames"].InnerText == "PopServerName")
     {
          string popserver = stats["KeyValues"].InnerText;//pop.gmail.com
     }
     if (stats["KeyNames"].InnerText == "PortNumber")
     {
          string portnumber = stats["KeyValues"].InnerText;//995
     }
}


I used this one. This is fast. Hope this will help you.
 
Share this answer
 
If you have a schema file (xsd), then you can drastically enhance performance by calling the BeginLoadData/EndLoadData for each table in your data set:

C#
foreach (DataTable dataTable in dataSet.Tables)
   dataTable.BeginLoadData();

dataSet.ReadXml("file.xml");

foreach (DataTable dataTable in dataSet.Tables)
   dataTable.EndLoadData();


see http://msdn.microsoft.com/en-us/library/fx29c3yd(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3[^]
 
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