C# 从 http 地址读取 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14160848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read an XML file from http address
提问by sd_dracula
I need to read an xml file using c#/.net from a source like so: https://10.1.12.15/xmldata?item=all
我需要使用 c#/.net 从这样的源读取一个 xml 文件: https://10.1.12.15/xmldata?item=all
That is basically just an xml file.
那基本上只是一个xml文件。
StreamReader does not like that.
StreamReader 不喜欢那样。
What's the best way to read the contents of that link?
阅读该链接内容的最佳方式是什么?
The file looks like so:
该文件如下所示:
- <RIMP>
- <HSI>
<SBSN>CZ325000123</SBSN>
<SPN>ProLiant DL380p Gen8</SPN>
<UUID>BBBBBBGGGGHHHJJJJ</UUID>
<SP>1</SP>
<cUUID>0000-000-222-22222-333333333333</cUUID>
- <VIRTUAL>...
采纳答案by Wim Ombelets
Another way to do this is using the XmlDocument
class. A lot of servers around the world are still running .Net Framework < 3.0 so it's good to know that this class still exists alongside XDocument
in case you're developing an application that will be run on a server.
另一种方法是使用XmlDocument
类。世界各地的许多服务器仍在运行 .Net Framework < 3.0,因此很高兴知道此类仍然存在XDocument
,以防您正在开发将在服务器上运行的应用程序。
string url = @"https://10.1.12.15/xmldata?item=all";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
回答by dtb
You'll want to use LINQ to XMLto process the XML file. The XDocument.Load Methodsupports loading an XML document from an URI:
您需要使用LINQ to XML来处理 XML 文件。该XDocument.Load方法支持加载从URI的XML文档:
var document = XDocument.Load("https://10.1.12.15/xmldata?item=all");
回答by Oscar R. Onorato
Maybe the correct answer must starting by reading the initial question about how to "Read an XML file from a URL (or in this case from a Http address)".
也许正确的答案必须从阅读关于如何“从 URL(或在这种情况下从 Http 地址)读取 XML 文件”的初始问题开始。
I think that can be the best for you see the next easy demos:
我认为这对你来说是最好的,看看下一个简单的演示:
(In this case XmlTextReader but today you can use XmlReader instead of XmlTextReader) http://support.microsoft.com/en-us/kb/307643
(在这种情况下 XmlTextReader 但今天你可以使用 XmlReader 而不是 XmlTextReader) http://support.microsoft.com/en-us/kb/307643
(Parallel you could read this documentation too). https://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx
(并行您也可以阅读此文档)。 https://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx
regards
问候