开发者

XML may not be returned as proper XML

开发者 https://www.devze.com 2023-04-05 16:43 出处:网络
I am trying to create a rest service that will return a list of XML elements with attributes containing information.

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 t开发者_如何学Che 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 &lt; instead of < and &gt; 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消