如何在 SilverLight 项目 (C#) 中使用 XDocument 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1021709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:43:05  来源:igfitidea点击:

How to Use XDocument class in SilverLight Project (C#)

c#xmlsilverlight

提问by Greg Zwaagstra

I'm trying to create a Silverlight application (for the first time) that involves parsing XML from a site and displaying information. To do this I am using Visual Studio 2008 on Windows XP Service Pack 3. I also have .NET Framework 3.5 SP1 installed.

我正在尝试创建一个 Silverlight 应用程序(第一次),它涉及从站点解析 XML 并显示信息。为此,我在 Windows XP Service Pack 3 上使用 Visual Studio 2008。我还安装了 .NET Framework 3.5 SP1。

My problem is that no XML-parser I have seen on the internet works. The top of my code I have both lines I believe are necessary (using "System.xml;" and using "System.linq;") but XDocument, XMLReader, XMLDocument, and any others I have found do not work, returning the error that the type or namespace cannot be found. I do have .NET Framework.

我的问题是我在互联网上看到的 XML 解析器没有工作。我的代码顶部有两行我认为是必要的(使用“System.xml;”和使用“System.linq;”)但 XDocument、XMLReader、XMLDocument 和我发现的任何其他行都不起作用,返回错误找不到类型或命名空间。我有.NET 框架。

I have turned absolutely nothing up on the internet regarding this problem. Does anyone have any ideas?

关于这个问题,我在互联网上完全没有发现任何东西。有没有人有任何想法?

EDIT: I just discovered that when I open the file outside of the context of a Silverlight project, it is able to use XDocument. It is only when I open the entire project that my problem occurs

编辑:我刚刚发现当我在 Silverlight 项目的上下文之外打开文件时,它能够使用 XDocument。只有当我打开整个项目时才会出现我的问题

Here is some sample code showing the problem:

下面是一些显示问题的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq; //Error 1 (See below)

namespace LastfmAmazon
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        public void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument doc = XDocument.Parse(e.Result); //Error 2: see below

        } 

        public void Button_Click(object sender, RoutedEventArgs e)
        {

            if (uname.Text != String.Empty)
            {
                App app = (App)Application.Current;
                app.UserName = uname.Text;
                String getTopArtists = "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=" + app.UserName + "&api_key=d2d620af554a60f228faed8d502c4936";
                uname.Text = "Try Another One!";
                WebClient web = new WebClient();
                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted);
                client.DownloadStringAsync(new Uri(getTopArtists));
            }
        }
    }   
}

Error 1: This line contains the following error: The type or namespace name 'Linq' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)

错误 1:此行包含以下错误:命名空间“System.Xml”中不存在类型或命名空间名称“Linq”(您是否缺少程序集引用?)

Error 2: This line contains the following error: The type or namespace name 'XDocument' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)

错误 2:此行包含以下错误:命名空间“System.Xml”中不存在类型或命名空间名称“XDocument”(您是否缺少程序集引用?)

EDIT 2: Once I Googled what it meant to "add a reference" to a library, Anthony's answer solved the problem.

编辑 2:一旦我在谷歌上搜索了“添加引用”到图书馆的含义,安东尼的回答就解决了这个问题。

采纳答案by AnthonyWJones

By default a Silverlight project will contain the System.Xml dll however XDcoument is contained in the System.Xml.Linq dll, this you will have to add to your project.

默认情况下,Silverlight 项目将包含 System.Xml dll,但 XDcoument 包含在 System.Xml.Linq dll 中,您必须将其添加到您的项目中。

回答by JaredPar

Make sure you add a reference to the appropriate XML library

确保添加对适当 XML 库的引用

  • For XMLDocument, XMLReader, etc ...: System.Xml.Dll
  • For XDocument, XNode, etc ...: System.Xml.Linq.dll
  • 对于 XMLDocument、XMLReader 等...:System.Xml.Dll
  • 对于 XDocument、XNode 等...:System.Xml.Linq.dll