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

如何在SilverLight项目(C#)中使用XDocument类,c#,xml,silverlight,C#,Xml,Silverlight,我正在尝试创建一个Silverlight应用程序(这是第一次),它涉及从站点解析XML并显示信息。为此,我在Windows XP Service Pack 3上使用Visual Studio 2008。我还安装了.NETFramework 3.5SP1 我的问题是,我在互联网上看到的XML解析器都不起作用。我的代码顶部有两行我认为是必要的(使用“System.xml;”和“System.linq;”),但XDocument、XMLReader、XMLDocument和我发现的任何其他行都不起作用

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

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

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

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

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

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));
            }
        }
    }   
}
错误1:此行包含以下错误:命名空间“System.Xml”中不存在类型或命名空间名称“Linq”(是否缺少程序集引用?)

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


编辑2:当我在谷歌上搜索到“添加引用”到库的含义时,Anthony的回答解决了这个问题。

确保您添加了对相应XML库的引用

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

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

想法1:创建一小段再现问题的代码,然后编辑您的问题以包含代码,还要添加您收到的完整错误消息。您可能只需要将它们作为项目的引用添加。小型Silverlight System.Xml dll没有XmlDocument对象,Silverlight中不支持该特定DOM。Xml唯一可用的DOM是XDocument。我还要注意,这个程序集位于引用管理器中的Assemblies->Extensions下,而不是Assemblies->Framework下。