如何在 SilverLight 项目 (C#) 中使用 XDocument 类 [英] How to Use XDocument class in SilverLight Project (C#)
问题描述
我正在尝试创建一个 Silverlight 应用程序(这是第一次),它涉及从站点解析 XML 并显示信息.为此,我在 Windows XP Service Pack 3 上使用 Visual Studio 2008.我还安装了 .NET Framework 3.5 SP1.
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.
我的问题是我在 Internet 上看到的 XML 解析器没有工作.我的代码顶部有两行我认为是必要的(使用System.xml;"和使用System.linq;")但 XDocument、XMLReader、XMLDocument 和我发现的任何其他行都不起作用,返回错误找不到类型或命名空间.我有 .NET 框架.
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.
关于这个问题,我在互联网上完全没有查到任何信息.有人有什么想法吗?
I have turned absolutely nothing up on the internet regarding this problem. Does anyone have any ideas?
我刚刚发现当我在 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));
}
}
}
}
错误 1:此行包含以下错误:命名空间System.Xml"中不存在类型或命名空间名称Linq"(您是否缺少程序集引用?)
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?)
错误 2:此行包含以下错误:命名空间System.Xml"中不存在类型或命名空间名称XDocument"(您是否缺少程序集引用?)
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:一旦我在谷歌上搜索到添加引用"到图书馆的含义,安东尼的回答就解决了这个问题.
EDIT 2: Once I Googled what it meant to "add a reference" to a library, Anthony's answer solved the problem.
推荐答案
默认情况下,Silverlight 项目将包含 System.Xml dll,但是 XDcoument 包含在 System.Xml.Linq dll 中,您必须将其添加到您的项目.
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 项目 (C#) 中使用 XDocument 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!