Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 VS2010
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 28-Feb-11 0:49am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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[^]
  Permalink  
Comments
Angsuman Chakraborty at 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 .
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Please check following link
 
Reading Xml with XmlReader in C#[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
<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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

If you have the XML file like
 
<?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
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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:
 
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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 318
1 Sergey Alexandrovich Kryukov 286
2 RyanDev 75
3 PhilLenoir 60
4 nv3 60
0 Sergey Alexandrovich Kryukov 6,691
1 OriginalGriff 6,119
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 9 Dec 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100