Loading XML from Web Service

Posted by Lukasz on Stack Overflow See other posts from Stack Overflow or by Lukasz
Published on 2010-04-28T18:21:41Z Indexed on 2010/04/28 18:27 UTC
Read the original article Hit count: 418

Filed under:
|
|
|

I am connecting to a web service to get some data back out as xml. The connection works fine and it returns the xml data from the service.

var remoteURL = EveApiUrl;

var postData = string.Format("userID={0}&apikey={1}&characterID={2}", UserId, ApiKey, CharacterId);

var request = (HttpWebRequest)WebRequest.Create(remoteURL);

request.Method = "POST";
request.ContentLength = postData.Length;
request.ContentType = "application/x-www-form-urlencoded";

// Setup a stream to write the HTTP "POST" data
var WebEncoding = new ASCIIEncoding();
var byte1 = WebEncoding.GetBytes(postData);
var newStream = request.GetRequestStream();

newStream.Write(byte1, 0, byte1.Length);
newStream.Close();

var response = (HttpWebResponse)request.GetResponse();

var receiveStream = response.GetResponseStream();

var readStream = new StreamReader(receiveStream, Encoding.UTF8);
var webdata = readStream.ReadToEnd();

Console.WriteLine(webdata);

This prints out the xml that comes from the service. I can also save the xml as an xml file like so;

TextWriter writer = new StreamWriter(@"C:\Projects\TrainingSkills.xml");
writer.WriteLine(webdata);
writer.Close();

Now I can load the file as an XDocument to perform queries on it like this;

var data = XDocument.Load(@"C:\Projects\TrainingSkills.xml");

What my problem is that I don't want to save the file and then load it back again. When I try to load directly from the stream I get an exception, Illegal characters in path. I don't know what is going on, if I can load the same xml as a text file why can't I load it as a stream.

The xml is like this;

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2010-04-28 17:58:27</currentTime>
  <result>
    <currentTQTime offset="1">2010-04-28 17:58:28</currentTQTime>
    <trainingEndTime>2010-04-29 02:48:59</trainingEndTime>
    <trainingStartTime>2010-04-28 00:56:42</trainingStartTime>
    <trainingTypeID>3386</trainingTypeID>
    <trainingStartSP>8000</trainingStartSP>
    <trainingDestinationSP>45255</trainingDestinationSP>
    <trainingToLevel>4</trainingToLevel>
    <skillInTraining>1</skillInTraining>
  </result>
  <cachedUntil>2010-04-28 18:58:27</cachedUntil>
</eveapi>

Thanks for your help!

© Stack Overflow or respective owner

Related posts about linq-to-xml

Related posts about Xml