progressbar + background worker + reading a large XML file

  • Thread starter Thread starter chadwell
  • Start date Start date
C

chadwell

Guest
I am trying to validate a large XML file against an XSD in C# code. The file is 70MB in size.

I would like to have a progress bar so the windows app does not appear to hang.

How can I implement the background worker and progress bar? The user can select any XML file and XSD, so the nodes will be completely different - so Im not sure how to count all the nodes to use as progress when I wont know the node names.


private void button3_Click(object sender, EventArgs e)
{
// create object for the XSD file that will be used
// for validating the XML file. Use the Connection
// Manager to get the path from the XSD file.
XmlReaderSettings xmlrs = new XmlReaderSettings();
xmlrs.ValidationType = ValidationType.Schema;
xmlrs.Schemas.Add(null, txtXSDPath.Text);
xmlrs.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
xmlrs.Schemas.Compile();

XmlDocument doc = new XmlDocument();
doc.Load(txtXMLPath.Text);
long filelength = doc.OuterXml.Length;




// Try reading the XML file using the XSD. Use the
// Connection Manager to get the path from the XML file
try
{
XmlReader xmlr = XmlReader.Create(txtXMLPath.Text, xmlrs);


while (xmlr.Read())
{
}
// The XML file was succesfully read.
//Dts.TaskResult = (int)ScriptResults.Success;
if (errorcount == 0)
{
txtValidated.Text += "The XML is valid";
}
}
catch (Exception ex)
{
// Validation failed

txtValidated.Text += Environment.NewLine + ex.Message + Environment.NewLine + "--------------------------------------";


}
}


//Display any warnings or errors.
private void ValidationCallBack(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
txtValidated.Text += (Environment.NewLine + "\tWarning: Matching schema not found. No validation occurred." + args.Message.ToString() + Environment.NewLine + "--------------------------------------");
else
txtValidated.Text += (Environment.NewLine + "\tValidation error: " + args.Message + Environment.NewLine + "--------------------------------------");
errorcount++;
}

Continue reading...
 
Back
Top