gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:53:40 25 4
gpt4 key购买 nike

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

我的问题是我在 Internet 上看到的 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:当我用 Google 搜索“添加引用”到库的含义后,Anthony 的回答解决了这个问题。

最佳答案

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

关于c# - 如何在 SilverLight 项目中使用 XDocument 类 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1021709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com