0

无论如何要阅读特定的行吗?

http://i.stack.imgur.com/hDPIg.jpg

                XDocument dataFeed = XDocument.Parse(e.Result);

                AchivementsListBox.ItemsSource = from query in dataFeed.Descendants("MaxPayne3")
                                                 select new NewGamesClass
                                                 {
                                                     GameGuide = (string)query.Element("Guide")

                                                 };
4

2 回答 2

0
// It Will Download The XML Once To Use further to Avoid Again And Again Calls For Each Time


public void GetXML(string path)
            {
                WebClient wcXML = new WebClient();
                wcXML.OpenReadAsync(new Uri(path));
                wcXML.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient);
            }

void webClient(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                try
                {
                    Stream Resultstream = e.Result;
                    XmlReader reader = XmlReader.Create(Resultstream);

                        var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
                        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Download.xml", System.IO.FileMode.Create, isolatedfile))
                        {
                            byte[] buffer = new byte[e.Result.Length];
                            while (e.Result.Read(buffer, 0, buffer.Length) > 0)
                            {
                                stream.Write(buffer, 0, buffer.Length);
                            }
                            stream.Flush();
                            System.Threading.Thread.Sleep(0);
                        }
                }

                catch (Exception ex)
                {
                    //Log Exception
                }
            }

            if (e.Error != null)
            {
                //Log Exception
            }
        }

// This Method Will Give You Required Info According To Your Tag Like "Achievement123"

protected List<DownloadInfo> GetDetailFromXML(string TagName)
        {
            //TagName Like "achivement23"
            List<DownloadInfo> listDetails = new List<DownloadInfo>();

            XDocument loadedData;
            try
            {
                using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Check For Islated Storage And Load From That
                    if (storage.FileExists("Download.xml"))
                    {
                        using (Stream stream = storage.OpenFile("Download.xml", FileMode.Open, FileAccess.Read))
                        {
                            loadedData = XDocument.Load(stream);
                            //TagName Like "achivement23"
                            listDetails.AddRange((from query in loadedData.Element("GOW1").Elements(TagName)
                                                  select new DownloadInfo
                                                  {
                                                      TITLE = (string)query.Element("TITLE"),
                                                      DESCRIPTION = (string)query.Element("DESCRIPTION"),
                                                      ACHIVEMENTIMAGE = (string)query.Element("ACHIVEMENTIMAGE"),
                                                      GUIDE = (string)query.Element("GUIDE"),
                                                      YOUTUBELINK = (string)query.Element("YOUTUBELINK")
                                                  }).ToList());
                        }
                    }
                }
                return listDetails;
            }
            catch (Exception ex)
            {
                return listDetails = null;
                //Log Exception
            }
        }

        public class DownloadInfo
        {
            public string TITLE { get; set; }
            public string DESCRIPTION { get; set; }
            public string GUIDE { get; set; }
            public string ACHIVEMENTIMAGE { get; set; }
            public string YOUTUBELINK { get; set; }
        }

这是您的方法调用

GetXML("ANY URL FROM WHERE YOU WANT TO GET XML"); // You Will Download All The XML Once To Avoid Again And Again Server Calls To Get XML. 

GetDetailFromXML("YOUR TAG NAME"); // It Will Take Your Tag Name Which Information You Want To Get Like "Acheicement123" and It Will Return You Data In List<DownloadInfo> and You can easily get data from this List.

我希望它会帮助你。我没有在运行时测试代码。但我希望它会给你一些想法.. :)

于 2012-07-15T18:50:27.863 回答
0

我将使用单个WebClient实例来下载文件,并将其简化为单个事件处理程序。更重要的是,据我所知,你有一个类似的文档结构。这意味着您还可以重复使用您的阅读代码,而不是编写两次。

像这样的东西:

void Download(GameType type, Action onCompletion)
{
    WebClient client = new WebClient();
    client.DownloadProgressChanged += (s, args) =>
    {
        // Handle change in progress.
    };

    client.DownloadStringCompleted += (s, args) =>
    {
        XDocument feed = XDocument.Parse(args.Result);

        var itemSource = from query in feed.Root
                                      select new NewGamesClass
                                      {
                                          NewGameTitle = (string)query.Element("Title"),
                                          NewGameDescription = (string)query.Element("Descript
                                          NewGameImage = (string)query.Element("Image")
                                      };
        switch (type)
        {
            case GameType.ComingSoon:
                {
                    ComingSoonList.ItemSource = itemSource;
                }
            case GameType.NewGames:
                {
                    NewGameList.ItemSource = itemSource;
                }

        }

        if (onCompletion != null)
            onCompletion();
    };

    switch (type)
    {
        case GameType.NewGames:
            {
                client.DownloadStringAsync(new Uri("http://microsoft.com", UriKind.Absolute));
                break;
            }
        case GameType.ComingSoon:
            {
                client.DownloadStringAsync(new Uri("http://www.bing.com", UriKind.Absolute));
                break;
            }
    }
}

虽然代码可能看起来有点复杂,但它允许您在下载特定数据集时递归下载数据。显然,您必须声明GameType枚举,我只是在这里使用了一堆测试值来演示这个想法。

于 2012-07-16T01:40:59.553 回答