繁体   English   中英

XDocument.Load 方法在 XDocument 中不存在

[英]XDocument.Load method does not exist in XDocument

我坚持使用 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 Community。

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

要调用静态方法,就是这样:

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

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

将代码更改为此:

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

XDocument.Load是一个静态方法,只需使用此方法即可(无需新方法):

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

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

另外,对于XDocumentstatic方法

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

对于XmlDocument

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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