从 Web 服务加载 XML
我正在连接到 Web 服务以获取一些以 xml 形式返回的数据。连接工作正常并且从服务返回 xml 数据。
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);
这会打印出来自服务的 xml。我还可以将 xml 保存为 xml 文件,如下所示;
TextWriter writer = new StreamWriter(@"C:\Projects\TrainingSkills.xml");
writer.WriteLine(webdata);
writer.Close();
现在我可以将文件作为 XDocument 加载以对其执行查询,如下所示;
var data = XDocument.Load(@"C:\Projects\TrainingSkills.xml");
我的问题是我不想保存文件然后再次加载它。当我尝试直接从流加载时,出现异常:路径中存在非法字符。我不知道发生了什么,如果我可以加载相同的 xml 作为文本文件,为什么我不能将其作为流加载。
xml是这样的;
<?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>
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您正在尝试使用:
尝试将其加载,就好像 XML 是文件名一样!
相反,您想要
替代地,使用
或
My guess is that you're trying to use:
That's trying to load it as if the XML was a filename!
Instead, you want
Alternatively, use
or