How to make a query within an XML file?

1

I need the value of <version> to be returned to a variable in a WPF application. The XML file is on the server and contains:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<Application>
<Version>1.2.3.5</Version>
<ZipFile>Atu_SGT_1.2.3.5.zip</ZipFile>
</Application> 

My question is how to query this XML to check the value of <version>1.2.3.5</version> .

    
asked by anonymous 26.03.2014 / 18:07

1 answer

2

The SelectSingleNode allows you to browse the XML file, according to its structure:

XmlDocument doc = new XmlDocument();
doc.Load("c:\seu_arquivo.xml");

XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
string version = node.InnerText;

If you want to bring the XML from an HTTP server, you can try loading it and then reading it in the same way:

var url = "http://seuservidor.com/seu_arquivo.xml";
var xmlVersion = (new WebClient()).DownloadString(url);

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlVersion);

XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
string version = node.InnerText;
    
26.03.2014 / 18:32