XML 可能不会作为正确的 XML 返回
我正在尝试创建一个 REST 服务,该服务将返回一个 XML 元素列表,其中的属性包含信息。
当我从浏览器运行此休息服务器时,我会正确显示浏览器返回的 XML。但是,当我在 Windows 窗体中运行它并尝试从 XML 中提取属性时,它只提取第一个属性。
这是向浏览器显示的 XML。
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<catalog version="1.1">
<dataset id="XXX" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
</catalog>
</string>
但是,当我将流转换为字符串并将 XML 显示到文本框时,我得到 <
而不是 <
和 >< /code> 而不是
>
。我认为这是因为我正在将其转换为字符串。
这是我必须检索 XML 的代码。
WebRequest restWebRequest = WebRequest.Create(url);
restWebRequest.Method = "GET";
restWebRequest.ContentType = "application/x-www-form-urlencoded";
// Send the web request, and get the response from
WebResponse response = restWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string responseFromServer = reader.ReadToEnd();
textBox1.Text = responseFromServer;
我尝试从responseStream 中提取XML 元素。
任何帮助都会很棒。
谢谢!
I am trying to create a rest service that will return a list of XML elements with attributes containing information.
When I run this rest server from a browser, I get properly displayed XML returned the the browser. However, When I run this in a windows form, and try to extract the attributes from the XML, it only extracts the first one.
Here is the XML as displayed to the browser.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<catalog version="1.1">
<dataset id="XXX" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
</catalog>
</string>
However, When i convert the stream to a string and display the XML to a textbox I get <
instead of <
and >
instead of >
. I assume this is because I am converting this to a string.
Here is the code I have to retrieve the XML.
WebRequest restWebRequest = WebRequest.Create(url);
restWebRequest.Method = "GET";
restWebRequest.ContentType = "application/x-www-form-urlencoded";
// Send the web request, and get the response from
WebResponse response = restWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string responseFromServer = reader.ReadToEnd();
textBox1.Text = responseFromServer;
I try to extract the XML elements from the responseStream.
Any help would be awesome.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,以下代码给出了正确的输出。
我认为您正在从数据库获取 XML。尝试使用 CDATA 标签。
我希望这些链接对您有用: C# 对象到 XML
http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx
for me following code is giving right output.
I think your are getting XML from database. try using CDATA Tag.
I hope these links will be useful for you: C# object to XML
http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx