【问题标题】:XDocument.Load method does not exist in XDocumentXDocument.Load 方法在 XDocument 中不存在
【发布时间】:2016-12-30 14:09:59
【问题描述】:

我一直在用 Linq 加载一个新的 XML 文档。这是我的代码:

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");
        }
    }
}

我得到的回报是“XDocument”类型上不存在“Load”方法。 这真的很奇怪,因为我认为“使用 System.Xml.Linq”就足够了。 我想使用 Linq,因为我有一个复杂的 XML,而且我认为使用 Linq 浏览所有元素更容易。我正在使用 Visual Studio 2015 社区。​​p>

【问题讨论】:

  • 解决了,我只是个假人。谢谢大家!

标签: c# .net xml linq linq-to-xml


【解决方案1】:

Load 是一个静态方法。您的代码在语法上不正确 - 您似乎在尝试调用构造函数(通过使用 new),但您缺少一些括号。

要调用静态方法,很简单:

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

【讨论】:

    【解决方案2】:

    LoadXDocumentstatic 方法。您的代码尝试实例化一个新的XDocument 对象(在new XDocument())并调用Load 作为其实例方法。

    将您的代码更改为:

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

    【讨论】:

      【解决方案3】:

      XDocument.Load是一个静态方法,直接这样使用(不用new):

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

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

      【讨论】:

        【解决方案4】:

        另外,对于XDocumentstatic方法

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

        对于XmlDocument

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多