10

I need to read an xml file using c#/.net from a source like so: https://10.1.12.15/xmldata?item=all

That is basically just an xml file.

StreamReader does not like that.

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>...
sd_dracula
  • 3,796
  • 28
  • 87
  • 158
  • 1
    [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Brian Driscoll Jan 04 '13 at 16:24
  • 1
    Possible Duplicate: http://stackoverflow.com/questions/4842038/streamreader-and-reading-an-xml-file – Eli Gassert Jan 04 '13 at 16:24
  • Read about [XDocument Methods](http://msdn.microsoft.com/en-us/library/bb301598.aspx) – huMpty duMpty Jan 04 '13 at 16:27
  • 1
    @EliGassert: That question is based on HttpWebRequest and XmlDocument -- two very dated options to work with HTTP and XML in C# respectively. – dtb Jan 04 '13 at 16:30
  • 1
    The top two answers are exactly what he's trying to do. The accepted answer reads the contents then loads the text into an XmlDocument; the second answer shows that you can load a document by URL, which is also the top-trending answer in this thread. – Eli Gassert Jan 04 '13 at 16:34

3 Answers3

15

You'll want to use LINQ to XML to process the XML file. The XDocument.Load Method supports loading an XML document from an URI:

var document = XDocument.Load("https://10.1.12.15/xmldata?item=all");
dtb
  • 213,145
  • 36
  • 401
  • 431
9

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 XDocumentin case you're developing an application that will be run on a server.

string url = @"https://10.1.12.15/xmldata?item=all";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • This works just fine. I am just a bit stuck at trying to read one node at a time. I have this: ` ` I just want to select the Serial Number value, how do I do that, `GetAttributeNode("NAME")` just gives back "Product Name" but I want the second NAME and VALUE for the serial number – sd_dracula Jan 04 '13 at 20:09
  • `XmlNode myNode = xmlDoc.GetSingleNode("//FIELD[@NAME='Serial Number']/@VALUE");` – Wim Ombelets Jan 04 '13 at 20:16
0

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)".

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

(Parallel you could read this documentation too). https://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx

regards