C#流读取添加\\\<br/>到XML [英] C# Stream Reader adding \n to XML

查看:543
本文介绍了C#流读取添加\\\<br/>到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用StreamReader类来获取XML从谷歌我的地理编码过程。

I use the StreamReader class to obtain XML for my GeoCoding process from Google.

StreamReader srGeoCode = new StreamReader(WebRequest.Create(Url).GetResponse().GetResponseStream());
String GeoCodeXml = srGeoCode.ReadToEnd();
XmlDocument XmlDoc = new XmlDocument();
GeoCode oGeoCode = new GeoCode();
XmlDoc.Load(GeoCodeXml);



我得到的XML回来,但它增加了\\\
和其他额外的XML

I get XML back but it adds \n and other extras to the XML

<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<kml xmlns=\"http://earth.google.com/kml/2.0\"><Response>\n  <name>



我在VB中的相同的代码,并没有做到这一点。我可以成功地地理编码使用VB版本的控制台应用程序的我的信息。

I have the same code in VB and it does not do this. I can successfully GeoCode my information using the VB version of this console app.

有一个原因,C#版本增加了这些额外的数据,我检索后面的XML?我想我最好的一切在转换为C#。我喜欢它了VB编码

Is there a reason the C# version adds this extra data to the XML that I retrieve back? I am trying my best to convert everything over to C#. I enjoy coding in it over VB.

下面是VB代码:

    Dim wreqGeoCode As WebRequest = WebRequest.Create(strURL)
    Dim wresGeoCode As WebResponse = wreqGeoCode.GetResponse
    Dim srGeoCode As New StreamReader(wresGeoCode.GetResponseStream())
    Dim strXML As String = srGeoCode.ReadToEnd()
    Dim xmlDoc As New XmlDocument
    xmlDoc.LoadXml(strXML)


推荐答案

您需要XmlDoc.LoadXml如果你要加载的字符串。加载从文件中装载。

You need XmlDoc.LoadXml if you're going to load a string. Load loads from a file.

BTW,另类也更有效率。您可以直接从流加载文档:

BTW, the alternative is also more efficient. You can load the document directly from the stream:

WebRequest webRequest = WebRequest.Create(Url);
using (WebResponse webResponse = webRequest.GetResponse())
{
    using (Stream responseStream = webResponse.GetResponseStream())
    {
        XmlDocument XmlDoc = new XmlDocument();
        GeoCode oGeoCode = new GeoCode();
        XmlDoc.Load(responseStream);
    }
}



使用语句确保 WebResponse类得到清理,即使抛出异常。

The using statements ensure that the WebResponse and Stream get cleaned up, even if an exception is thrown.

这篇关于C#流读取添加\\\<br/>到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆