2009-06-20 20 views
5

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

我的问题是,我没有看到在互联网上的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:此行包含以下错误:类型或命名空间名称“ Linq'不存在于命名空间'System.Xml'中(您是否缺少程序集引用?)

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

编辑2:一旦我谷歌搜索“图书馆添加参考”意味着什么,安东尼的答案解决了这个问题。

+0

想法1:创建一小段重现问题的代码,然后编辑您的问题以包含代码,同时添加您收到的完整错误消息。 – 2009-06-20 14:28:53

+0

您可能只需要将它们添加为您项目的参考。 – 2009-06-20 14:29:17

回答

9

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

3

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

  • 对于为XMLDocument,XMLReader的,等...:system.xml.dll的
  • 有关的XDocument,XNode,等...:系统.Xml.Linq.dll
+2

小问题,Silverlight System.Xml dll没有XmlDocument对象,Silverlight中没有支持该特定的DOM。唯一可用于Xml的DOM是XDocument。 – AnthonyWJones 2009-06-20 21:44:49

相关问题