简体   繁体   中英

XDocument.Load method does not exist in XDocument

I'm stuck at loading a new XML document with Linq. That's my code:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
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.Xml.Linq;

namespace Project
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            XDocument doc = new XDocument.Load("001.xml");
        }
    }
}

What I get in return is that 'Load' method does not exists on type 'XDocument'. It's really weird because I thought that "using System.Xml.Linq" was enough. I wanted to use Linq because I have a complex XML and I think it's easier to navigate through all elements with Linq. I am using Visual Studio 2015 Community.

Load is a static method. Your code isn't syntactically correct - you're seemingly attempting to call a constructor (by using new ), but you're missing some brackets.

To call a static method, it's simply this:

var doc = XDocument.Load("001.xml");

Load is a static method of XDocument . Your code tries to instantiate a new XDocument object (at new XDocument() ) and call Load as its instance method.

Change your code to this:

XDocument doc = XDocument.Load("001.xml"); // without "new"

XDocument.Load is a static method, simply use it this way (without the new):

XDocument doc = XDocument.Load("001.xml");

https://msdn.microsoft.com/en-us/library/bb343181(v=vs.110).aspx

In addition, For static method of XDocument

 var xmlDoc = XDocument.Load("./wwwroot/resources/createwebsite.xml");

For XmlDocument

  var xmld = new XmlDocument();
  xmld.Load("./wwwroot/resources/createwebsite.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM