How to make a query inside an XML file?

Asked

Viewed 319 times

1

I need the value of <version> is 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 verify the value of <version>1.2.3.5</version>.

  • 1

    Are you using any language to make this query? Do you have any code ready? Please specify a little more your question so we can help.

  • Ok. The application is in WPF. The xml file is on the server. My question is how I can query this xml to check the <version>1.2.3.5</version>. Thank you

  • Ok. The application is a WPF in C#. The xml file is on the server. My question is how I can query this xml to check the <version>1.2.3.5</version>. I think of a method that would do this and it is called in the application’s Window_loaded. Thanks for helping .

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 XML from an HTTP server, you can try to load it and then read it 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.