Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use XDocument class in SilverLight Project (C#)

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.

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?

EDIT: I just discovered that when I open the file outside of the context of a Silverlight project, it is able to use XDocument. It is only when I open the entire project that my problem occurs

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));
            }
        }
    }   
}

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?)

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?)

EDIT 2: Once I Googled what it meant to "add a reference" to a library, Anthony's answer solved the problem.

like image 920
Greg Zwaagstra Avatar asked Jun 20 '09 14:06

Greg Zwaagstra


2 Answers

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.

like image 117
AnthonyWJones Avatar answered Oct 22 '22 22:10

AnthonyWJones


Make sure you add a reference to the appropriate XML library

  • For XMLDocument, XMLReader, etc ...: System.Xml.Dll
  • For XDocument, XNode, etc ...: System.Xml.Linq.dll
like image 37
JaredPar Avatar answered Oct 22 '22 22:10

JaredPar