Note that there are some explanatory texts on larger screens.

plurals
  1. POSpeed up parsing of XML in C# Compact Framework (using XmlTextReader & XElement)?
    text
    copied!<p>I am trying to import large XML file (goal is 100,000 rows) to import it to SQL CE database file (with Compact Framework 3.5 and SQL CE 3.5).</p> <p>Recently I wrote some code to do that, where I use XmlTextReader with XElement to parse my XML input file and then SqlCeCommand.TaableDirect/SqlCeResultSet/Insert to write it to database file. (Complete code below)...</p> <p>At the moment I am doing test with ~25,000 rows file and my performance get bottlenecked at line which parse each line of my input XML file.</p> <pre><code>xElem = XElement.Parse(xmlTextReader.ReadOuterXml()); </code></pre> <p>Performing some runs on emulator... It turns out that running a complete code takes 93 seconds, running empty if statement inside while loop (with everything commented out) takes 14 seconds, and running only XElement.Parse in same if statement takes 60 seconds. So XElement.Parse takes about half of what it takes to run the entire code (46 seconds vs. 93). Same was observed on real devices (not emulator).</p> <p>What can I do to speed this up? xmlTextReader.ReadOuterXml() consists of following: <code>&lt;item&gt;&lt;Index&gt;121fg12e&lt;Index&gt;&lt;Name&gt;John&lt;/Name&gt;.........&lt;Notes&gt;John's profile&lt;/Notes&gt;&lt;/item&gt;</code> (where I just basically want to extract values inside tags).</p> <p>Below is my complete code: </p> <pre><code>XmlTextReader xmlTextReader = new XmlTextReader(modFunctions.InFName); XElement xElem = new XElement("item"); using (SqlCeConnection cn = new SqlCeConnection(connectionString)) { if (cn.State == ConnectionState.Closed) cn.Open(); using (SqlCeCommand cmd = new SqlCeCommand()) { cmd.Connection = cn; cmd.CommandText = "item"; cmd.CommandType = CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable)) { SqlCeUpdatableRecord record = rs.CreateRecord(); while (xmlTextReader.Read()) { if (xmlTextReader.NodeType == XmlNodeType.Element &amp;&amp; xmlTextReader.LocalName == "item" &amp;&amp; xmlTextReader.IsStartElement() == true) { xElem = XElement.Parse(xmlTextReader.ReadOuterXml()); values[0] = (string)xDoc.Root.Element("Index"); // 0 values[1] = (string)xDoc.Root.Element("Name"); // 1 ~~~ values[13] = (string)xDoc.Root.Element("Notes"); // 13 for (int i = 0; i &lt; values.Length; i++) { if(!string.IsNullOrEmpty(values[i])) record.SetValue(i, values[i]); // 0 } rs.Insert(record); } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload