XML 可能不会作为正确的 XML 返回

发布于 2024-12-05 13:54:29 字数 1451 浏览 0 评论 0原文

我正在尝试创建一个 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 显示到文本框时,我得到 &lt; 而不是 <&gt;< /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 技术交流群。

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

发布评论

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

评论(1

滥情哥ㄟ 2024-12-12 13:54:29

对我来说,以下代码给出了正确的输出。

WebRequest restWebRequest = WebRequest.Create(@"C:\TestProjects\WebApplication4\WebApplication4\XMLFile1.xml");
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;

我认为您正在从数据库获取 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.

WebRequest restWebRequest = WebRequest.Create(@"C:\TestProjects\WebApplication4\WebApplication4\XMLFile1.xml");
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 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

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