C# Stream Reader 将 \n 添加到 XML

发布于 2024-09-02 01:12:48 字数 1013 浏览 7 评论 0原文

我使用 StreamReader 类从 Google 获取用于地理编码过程的 XML。

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 中添加了 \n 和其他额外内容。

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

我在 VB 中有相同的代码,但它没有这样做。我可以使用此控制台应用程序的 VB 版本成功对我的信息进行地理编码。

C# 版本将此额外数据添加到我检索回的 XML 中是否有原因?我正在尽力将所有内容都转换为 C#。与 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)

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);

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>

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.

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.

Here is the VB Code:

    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

黯然 2024-09-09 01:12:48

如果要加载字符串,则需要 XmlDoc.LoadXml。从文件加载负载。


顺便说一句,替代方案也更有效。您可以直接从流加载文档:

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);
    }
}

using 语句确保 WebResponseStream 得到清理,即使抛出异常也是如此。

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


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);
    }
}

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

墨小墨 2024-09-09 01:12:48

则不只是这样做。

   GeoCodeXml=GeoCodeXml.Replace("\n","");

如果它确实返回此处提到的 \n ,

y not just do

   GeoCodeXml=GeoCodeXml.Replace("\n","");

if it is truly returning the \n as mentioned here.

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