Reputation: 552
I have to read XML file located on a website (currently still local). I'm using C# on windows form application, and I use the following code:
try
{
DataSet dsMain = new DataSet();
dsMain.ReadXml(txtUrl.Text);
}
catch (Exception exx)
{
MessageBox.Show(exx.Message);
}
Those code runs well, but the problem is dsMain.ReadXml() method is slow at first connection to the website. To prove this, i surround it with Stopwatch like below:
try
{
Stopwatch st = new Stopwatch();
st.Start();
DataSet dsMain = new DataSet();
dsMain.ReadXml(txtUrl.Text);
st.Stop();
MessageBox.Show(Math.Round(st.Elapsed.TotalSeconds, 2).ToString(), "XML reading cost");
}
catch (Exception exx)
{
MessageBox.Show(exx.Message);
}
The message box showed about 2-3 seconds for first loading, and about 0-0.01 second for every next reading during the application. If I close the application and run it again, this problem occur again. FYI, the XML file is small (under 10 KB).
So the question is, why DataSet.ReadXml() method is slow for first reading but fast for every next reading? How to speed up this method? Is there any code improvement I should add?
Upvotes: 1
Views: 819
Reputation: 7277
Probably because it tries to parse (or infer) the schema from the xml file. At a later stage (parsing a file for the second time) it doesn't create a schema anymore, but just adds the data to the table.
https://msdn.microsoft.com/en-us/library/360dye2a(v=vs.110).aspx
The ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. To read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.
...
If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. If the schema cannot be extended through inference in order to expose all data, an exception is raised.
Upvotes: 0
Reputation: 109130
It is slow the first time because the runtime is generating, dynamically, the code to do the de-serialisation.
To avoid this just use one of the .NET XML parsers directly into your own data structures optimised for your data (DataSet
itself adds a lot of overhead by being dynamic and having a generalised interface).
Upvotes: 2