Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...