[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

WebClient returning an XDocument

Posted on 2011-09-21
8
Medium Priority
?
942 Views
Last Modified: 2013-12-27
I'm writing a simple Windows Mobile application (my first) and just want to be sure of how best to obtain data from a web service.  Actually I'm not using a Web Service as such just a web page as I want to later port to Android so want to stay away from Microsoft specific functionality if I can.

My first problem.question is how best to return an XDocument from a web page being used as a service.  My code here is :-

XDocument xDocument = new XDocument();
xDocument.Declaration = new XDeclaration("1.0", "utf-8", "");
XElement xMainElement = new XElement("whereabouts");
xDocument.Add(xMainElement);

// Check login parameters
string username = Request.QueryString["uid"];

// Build Document

// return document
Response.Write(xDocument);

Open in new window


This is in page_load - essentially I want to return some data to the windows mobile WebClient to say if the user logged in or not.  The problem here is that I get the web page text following the XML - I assume that I just need to add Reponse.End() after the write but just want to be sure.

In the Windows Mobile Client I am using WebClient.OpenReadSync.  In my read completed function I have the following code L-

 
XDocument xDocument = new XDocument();
xDocument.Declaration = new XDeclaration("1.0", "utf-8", "");
XElement xMainElement = new XElement("whereabouts");
xDocument.Add(xMainElement);

// Check login parameters
string username = Request.QueryString["uid"];

// Build Document

// return document
Response.Write(xDocument);

Open in new window


Is this correct or is there a better way of getting the returned XML into an XDocument - I did try loading straight from the stream but this gave an error.
Stream reply = null;
StreamReader stream = null;

try
{
 reply = e.Result;
 stream = new StreamReader(reply);

// Read the reply into an XDocument
  string xml = stream.ReadToEnd();
  XDocument jobsDocument = XDocument.Load(xml);

}
finally
{
// Close the stream reader and stream
  if (stream != null)
	stream.Close();
  if (reply != null)
       reply.Close ();
}

Open in new window

0
Comment
Question by:ChrisMDrew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 36574445
For your serice:

XmlTextReader reader = new XmlTextReader(YOUR_XML_URL);

            // Supply the credentials necessary to access the Web server.
            XmlUrlResolver resolver = new XmlUrlResolver();
            resolver.Credentials = CredentialCache.DefaultCredentials;
            reader.XmlResolver = resolver;
 
            _xd.Load(reader);

0
 

Author Comment

by:ChrisMDrew
ID: 36574497
I have seen that you can load directly like that however I really need to use Async mode I believe to prevent hanging the phone waiting for a reply from the server.  

As such I am using the WebClient.OpenReadAsync rather than going direct as you suggest.

the Response.End has helped - I also had to change the code in the web page to return the data as

Response.Write(xDocument.Declaration.ToString() + xDocument.ToString());
Response.End();

Oddly enough when I load into the XDocument at the phone end I still get an error stating that it 'cannot find file'???
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 36574506
What Mobile OS are you using?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ChrisMDrew
ID: 36574637
Windows Mobile 7 - I think I have cracked the original question though - when i write the XDocument I need to use :-


Response.Write( @"<?xml version='1.0' encoding='utf-8'?>" + xDocument.ToString());
Response.End();

at the other end I use :-

string xml = stream.ReadToEnd();
XDocument jobsDocument = XDocument.Parse(xml);

which appears to work - is this the best way to do this however?

0
 
LVL 48

Expert Comment

by:Mikal613
ID: 36574700
When you add the web reference to the mobile project it automatically add the Async calls for you.
0
 

Author Comment

by:ChrisMDrew
ID: 36576889
...but as I metioned at the start - I don't want to use Web References / services (at least not the Microsoft .NET implementation) as I want to be able to port to Android at some stage.

Actually working fine as per my last comment - now for some LINQ questions...
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 36577193
Yes you can use the web client to DownloadSrring()
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 2000 total points
ID: 36580052
using System.Net;
using System.Xml;
using System.Threading;
using System.Xml.Linq;

 WebClient wc = new WebClient();
 wc.DownloadStringCompleted +=new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
 wc.DownloadStringAsync(new Uri("PathToYourXML")));


void  wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
      XDocument _xd;
     _xd = XDocument.Parse(e.Result, LoadOptions.None);
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question