从 Web 服务加载 XML

发布于 2024-08-30 06:44:17 字数 2144 浏览 2 评论 0原文

我正在连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-09-06 06:44:19

我的猜测是,您正在尝试使用:

string xml = GetXmlFromService();
XDocument doc = XDocument.Load(xml);

尝试将其加载,就好像 XML 是文件名一样!

相反,您想要

XDocument doc = XDocument.Parse(xml);

替代地,使用

XDocument doc = XDocument.Load(textReader);

XmlReader reader = XmlReader.Create(stream);
XDocument doc = XDocument.Load(reader);

My guess is that you're trying to use:

string xml = GetXmlFromService();
XDocument doc = XDocument.Load(xml);

That's trying to load it as if the XML was a filename!

Instead, you want

XDocument doc = XDocument.Parse(xml);

Alternatively, use

XDocument doc = XDocument.Load(textReader);

or

XmlReader reader = XmlReader.Create(stream);
XDocument doc = XDocument.Load(reader);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文