• Advertisement
Sign in to follow this  

[.net] XML Problem... probably an easy solution...

This topic is 4522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the following example XML file:
<?xml version="1.0" encoding="iso-8859-1"?>
<myapp>
	<games>
		<game name="Age of Empires - The Rise of Rome">
			<icon>aoer.ico</icon>
			<location>SOFTWARE\\Microsoft\\Microsoft Games\\Age of Empires Expansion\\1.0\\</location>
			<value>EXE Path</value>
			<launcher>EmpiresX.exe</launcher>
		</game>
        </games>
</myapp>

I'm using C# 2005 and XmlReader/XmlTextReader I'm trying to write an XML reader to read and display the values of "icon", "location", etc. but how do I access these elements? Are they subelements or something? Basically it will scan the location in the registry, if it exists it will put the name of the app in a listview control with it's respective icon. So far my app scans through and it finds the name attribute but I don't know how to scan in the subelements? MessageBox.Show(xmlReader.GetAttribute("name")); This displays the name, for example in this case a messagebox will appear saying "Age of Empires - The Rise of Rome" I tried using xmlReader.ReadElementString("location") to get the location but it says it wasn't there :S Hmmm... I hope I'm making sense.

Share this post


Link to post
Share on other sites
Advertisement
Ok, first learn XPath
Then use the following code:

XmlDocument doc = new XmlDocument();
doc.Load(...);
XmlNode iconNode = doc.DocumentElement.SelectSingleNode("/myapp/games/game[@name='Age of Empires - The Rise of Rome']/icon");




Cheers

Share this post


Link to post
Share on other sites
Either you want the above xpath. Or you can avoid xpath and use a dataset provided by Systems.Data; namespace. All you do is Declare your dataset and call its xmlread method. You can also alter data like that as well. I had code lying around but it looks like I lost it. It is not hard to figure out tho.

Share this post


Link to post
Share on other sites
Learn XPath. Using a dataset to read XML requires your XML to have a schema that matches what is expected for a serialized dataset. XmlDocument.SelectNode|SelectSingleNode is definitely the way to go. Using an XmlReader is just doing it the hard way (XmlDocument and XPathDocument are both built on top of XmlReader).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement