尝试使用 C# 读取和写入文件时 WPF 出错
Error in WPF when trying to read and write from a file using C#
所以我正在尝试读写 from/in .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?)
行:
document.load("MyXmlFile.xml");
代码示例:
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");
}
}
你得稍微改变一下你的方法:
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
所以我正在尝试读写 from/in .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?)
行:
document.load("MyXmlFile.xml");
代码示例:
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");
}
}
你得稍微改变一下你的方法:
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