C# Stream Reader 将 \n 添加到 XML
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要加载字符串,则需要 XmlDoc.LoadXml。从文件加载负载。
顺便说一句,替代方案也更有效。您可以直接从流加载文档:
using
语句确保WebResponse
和Stream
得到清理,即使抛出异常也是如此。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:
The
using
statements ensure that theWebResponse
andStream
get cleaned up, even if an exception is thrown.则不只是这样做。
如果它确实返回此处提到的 \n ,
y not just do
if it is truly returning the \n as mentioned here.