【发布时间】:2011-04-10 16:21:32
【问题描述】:
我正在尝试从外部网站传递一些 XML。
通过 c# 网页或 asp.MVC 执行此操作的最佳方法是什么?
【问题讨论】:
标签: c# asp.net asp.net-mvc xml webpage
我正在尝试从外部网站传递一些 XML。
通过 c# 网页或 asp.MVC 执行此操作的最佳方法是什么?
【问题讨论】:
标签: c# asp.net asp.net-mvc xml webpage
两者都应该没问题。 MVC 可能最简单(就获取原始响应而言),但您可以在常规 ASP.NET 中通过使用处理程序(可能是 .ashx)或仅通过清除响应来执行相同操作。
【讨论】:
我倾向于使用这样的东西来处理外部 XML 文档/RSS 提要等:
string sURL = ".....";
// Create a request for the URL.
WebRequest oRequest = WebRequest.Create(sUrl);
// Get the response.
WebResponse oResponse = oRequest.GetResponse();
// Get the stream containing content returned by the server.
Stream oDataStream = oResponse.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader oReader = new StreamReader(oDataStream, System.Text.Encoding.Default);
// Read the content.
string sXML = oReader.ReadToEnd();
// Convert string to XML
XDocument oFeed = XDocument.Parse(sXML);
【讨论】: