2016-04-28 23 views
0

,所以我想讀和/寫.xml文件和我得到這個錯誤:WPF C#閱讀和從.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; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.IO;   
using System.Xml.Linq;  // I included this for XDocument 
using System.Xml.XPath;  // I included this because i thought it will fix a problem 
    namespace WpfApplication1 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    public void loadXML() 
    { 
     XDocument document = new XDocument(); 

     if (!File.Exists("MyXmlFile.xml")) 
     { 

      document.Save("MyXmlFile.xml"); 
     } 
     else 
     { 
      //We know it exists so we can load it 
      document.load("MyXmlFile.xml"); 
     } 

     //Continue to work with document 


    } 


} 
} 

回答

2

你必須改變你的方法一點:

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 
} 
+0

改成了加載和我有一個新的錯誤: 會員「System.Xml.Linq.XDocument.Load(字符串)」不能用一個實例引用來訪問;使用類型名稱代替它 –

+0

@ C-PROG,是的,你是對的,請參閱編輯答案 –

+0

當我嘗試第二次運行它時,我得到一個減弱的說法: 根元素缺失。 任何提示? –