i need so input with xml and webclient in wp7
-
21-06-2021 - |
Pergunta
Is there anyway to read the specific line?
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")
};
Solução
I would use a single WebClient
instance to download the files, and simply reduce it to a single event handler. More than that, you have a similar document structure from what I can tell. That means that you could also re-use your reading code instead of writing it twice.
Something like this:
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;
}
}
}
Although the code might look a bit more complex, it lets you recursively download data when a specific data set is downloaded. Obviously, you would have to declare the GameType
enum and I simply used a bunch of test values here to demonstrate the idea.
Outras dicas
// 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; }
}
Here is Your Method Calls
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.
I hope it will help you. I didnot Test The Code on Runtime. But I Hope It Will Give You Some Idea.. :)