C# 连接到提供 xml 文档的 URL

发布于 2024-09-30 01:35:33 字数 57 浏览 0 评论 0原文

我如何在 C# 中调用一个给我一个 xml 文件的 URL,然后处理该 xml 文件,例如进行解析。

How can I in C# call a URL which gives me a xml file, and then process that xml file for example for parsing.

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

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

发布评论

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

评论(2

在巴黎塔顶看东京樱花 2024-10-07 01:35:33

要将 XML 文件下载到您的硬盘上,您只需执行以下操作即可。

XDocument doc = XDocument.Load(url);
doc.Save(filename);

如何解析它是另一回事,有几种不同的方法可以做到这一点。这是一个问题涵盖了主题。您还可以查看 MSDN 上的 LINQ to XML 参考。

To download an XML file onto your hard disk you can simply do.

XDocument doc = XDocument.Load(url);
doc.Save(filename);

How you parse it is a different matter and there are several different ways to do it. Here is a SO question that covers the subject. You can also checkout the LINQ to XML reference on MSDN.

生死何惧 2024-10-07 01:35:33
using System; 
using System.IO; 
using System.Net; 
using System.Text; 

... 

    public static void GetFile 
            ( 
            string strURL, 
            string strFilePath 
            ) 
        { 

            WebRequest myWebRequest = WebRequest.Create(strURL);  

            WebResponse myWebResponse = myWebRequest.GetResponse();  

            Stream ReceiveStream = myWebResponse.GetResponseStream(); 

            Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 

            StreamReader readStream = new StreamReader( ReceiveStream, encode ); 

            string strResponse=readStream.ReadToEnd(); 

            StreamWriter oSw=new StreamWriter(strFilePath); 

            oSw.WriteLine(strResponse); 

            oSw.Close(); 

            readStream.Close(); 

            myWebResponse.Close(); 

        } 

来自:http://zamov.online.fr/EXHTML/CSharp/CSharp1.html

XML 解析器:

http://www .c-sharpcorner.com/uploadfile/shehperu/simplexmlparser11292005004801am/simplexmlparser.aspx

只需将流传递到 XML 解析器即可。

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

... 

    public static void GetFile 
            ( 
            string strURL, 
            string strFilePath 
            ) 
        { 

            WebRequest myWebRequest = WebRequest.Create(strURL);  

            WebResponse myWebResponse = myWebRequest.GetResponse();  

            Stream ReceiveStream = myWebResponse.GetResponseStream(); 

            Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 

            StreamReader readStream = new StreamReader( ReceiveStream, encode ); 

            string strResponse=readStream.ReadToEnd(); 

            StreamWriter oSw=new StreamWriter(strFilePath); 

            oSw.WriteLine(strResponse); 

            oSw.Close(); 

            readStream.Close(); 

            myWebResponse.Close(); 

        } 

from : http://zamov.online.fr/EXHTML/CSharp/CSharp1.html

XML Parser:

http://www.c-sharpcorner.com/uploadfile/shehperu/simplexmlparser11292005004801am/simplexmlparser.aspx

Just pass the stream to the XML Parser.

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