2016-04-28 6 views
0

を読む&書く:ラインのWPF C#は、私が読んおよび.xmlファイルに/から記述しようとしていますし、私はこのエラーを取得するの.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

を参照してください.2回目に実行しようとすると、次のようになります。 ルート要素がありません。 ヒント? –

関連する問題