Using FileSystemWatcher in detecting a xml file, using Linq in reading the xml file and prompt the results error "Root Element is Missing"
Posted
by
GrayFullBuster
on Stack Overflow
See other posts from Stack Overflow
or by GrayFullBuster
Published on 2012-11-23T22:44:22Z
Indexed on
2012/11/23
23:04 UTC
Read the original article
Hit count: 212
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);
© Stack Overflow or respective owner