Using FileSystemWatcher in detecting a xml file, using Linq in reading the xml file and prompt the results error "Root Element is Missing"
- by GrayFullBuster
My application is already working it can detect the xml file and prompt the content of the xml file but sometimes it will prompt "Root element is missing", and sometimes also it is okay but when I open the xml file, it is ok, it has contents on it. How to solve this issue.
Here is the screenshot of the error:
Here is the code:
private void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
string invoice = "";
using (var stream = System.IO.File.Open(e.FullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
var doc = System.Xml.Linq.XDocument.Load(stream);
var transac = from r in doc.Descendants("Transaction")
select new
{
InvoiceNumber = r.Element("InvoiceNumber").Value,
};
foreach (var i in transac)
{
invoice = i.InvoiceNumber;
}
}
MessageBox.Show(invoice);
fileSystemWatcher.EnableRaisingEvents = false;
}
The error goes here var doc = System.Xml.Linq.XDocument.Load(stream);