我正在尝试创建一个Silverlight应用程序(第一次),其中涉及从站点解析XML并显示信息。为此,我在Windows XP Service Pack 3上使用Visual Studio 2008。我还安装了.NET Framework 3.5 SP1。
我的问题是,我看到的所有XML解析器都不起作用。在我的代码顶部,我认为需要以下两行(使用“System.xml;”和使用“System.linq;”),但XDocument、XMLReader、XMLDocument和任何其他我找到的都不起作用,并返回类型或命名空间无法找到的错误。我确实安装了.NET Framework。
我在互联网上完全没有发现有关这个问题的任何信息。 有人有什么想法吗?
编辑:我刚刚发现当我在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:我谷歌了一下如何“添加引用”到一个库,安东尼的回答解决了问题。