C# 尝试使用C从文件读写时WPF中出错#

C# 尝试使用C从文件读写时WPF中出错#,c#,xml,wpf,visual-studio-2010,C#,Xml,Wpf,Visual Studio 2010,因此,我正在尝试从/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 a

因此,我正在尝试从/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

您必须稍微改变一下您的方法:

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

将其更改为Load,我得到一个新错误:无法使用实例引用访问成员“System.Xml.Linq.XDocument.Load(string)”;用类型名来限定它,而不是代替当我第二次尝试运行它时,我得到一个逐渐减弱的消息:根元素丢失了。有什么提示吗?@C-PROG,这是因为您的文档是空的,并且不包含任何元素,如果您添加一些元素(即),一切都会正常工作。将其更改为加载,我发现一个新错误:无法使用实例引用访问成员“System.Xml.Linq.XDocument.Load(string)”;用类型名来限定它,而不是代替当我第二次尝试运行它时,我得到一个逐渐减弱的消息:根元素丢失了。有什么提示吗?@C-PROG,这是因为您的文档是空的,不包含任何元素,如果您添加一些元素(即),一切都会正常工作