私のアプリケーションはすでに動作しており、xml ファイルを検出して xml ファイルの内容を表示することができますが、「ルート要素が見つかりません」というメッセージが表示されることもあれば、問題ないこともありますが、xml ファイルを開くと問題ありません。内容があります。この問題を解決する方法。
エラーのスクリーンショットは次のとおりです。
コードは次のとおりです。
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;
}
ここにエラーが表示されますvar doc = System.Xml.Linq.XDocument.Load(stream);