[英]Error in WPF when trying to read and write from a file using C#
So I am trying to read and write from/in .xml file and I get this error:所以我试图从/在 .xml 文件中读取和写入,但出现此错误:
'System.Xml.Linq.XDocument' does not contain a definition for 'load' and no extension method 'load' accepting a first argument of type 'System.Xml.Linq.XDocument' could be found (are you missing a using directive or an assembly reference?)
for line:对于线:
document.load("MyXmlFile.xml");
Code Sample:代码示例:
using System.Xml.Linq; // I included this for XDocument
using System.Xml.XPath; // I included this because I thought it will fix a problem
public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
public void LoadXML()
{
var document = new XDocument();
if (!File.Exists("MyXmlFile.xml"))
document.Save("MyXmlFile.xml");
else document.load("MyXmlFile.xml");
}
}
You have to change your method a little:你必须稍微改变你的方法:
if (!File.Exists("MyXmlFile.xml"))
{
document.Save("MyXmlFile.xml");
}
else
{
//We know it exists so we can load it
document = XDocument.Load("MyXmlFile.xml"); // changed
}
//Continue to work with document
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.