XML fixing escaped entities



I read an xml file if there is an escape entity I would like to fix that before I do some processing and write the xml file back. Here is what I'm doing:



try
{

var doc = new XmlDocument();
// do some job

}

catch (Exception e)
{
if (e is XmlException)
{
checkXML(doc);
}
else
{
// do some job
}
}

finally
{

_watcher.EnableRaisingEvents = true;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}

public void checkXML(XmlDocument doc)
{
XmlNode node;

node = doc.SelectSingleNode("/BroadcastMonitor/Current");
node["name1"].InnerText = SecurityElement.Escape(node["name1"].InnerText);
node["name2"].InnerText = SecurityElement.Escape(node["name2"].InnerText); ;


}




My First question: I can't access the doc instance in the catch. How do I access the doc instance to pass it to checkXML


My Second question: After fixing the escape elements I want to return to the try block to do continue with the job that needs to be done on the xml file. How is this possible, or do I use nested try / catch.


Kindly explain. Thank you.


No comments:

Post a Comment