Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I was searching around and read a few books, to parse XML using C#. Unfortunately, none of them gave a very good explaination of how to parse it. What I want to do is parse the following XML file http://www.goamericasarmy.com/manager/status/xml.asp?server={A2D18978-64EE-41F9-B274-6BB127ABA593} with C#, but only pull out the values that I need. For example, I want to extract the IP address, the hostname, and all of the current players names. I have a good idea on how the elements work, but not the data inside them. For example I had used the Xml object in C# and could get the IP address, but after that I had no such luck. It seems as though you can't just search under a specific element for a piece of data with the tag "blah". For example Getting the data in VALUE in the element ipaddress (so that I would get the IP Address). If anyone has idea or knows of some sample code, that would be great.
-Sean
  • *Experts*
Posted

Can you show us what code you've used so far?

 

If you have an XmlDocument loaded, use the SelectSingleNode or SelectNodes methods to get to the nodes you want. For example, to get the ip address, use something like this:

XmlNode node = xml.SelectSingleNode("/STATUS/SERVERINFO/VARIABLE[@name='ipaddress']/Value");
string ipAddress = node.InnerText;

 

The XPATH string says this, mosly:

Find a node/element STATUS

Find a node/element SERVERINO under that (must be under it)

Find a node/element VARIABLE under that

The element VARIABLE must have an attribute named "name" that has the value "ipaddress". The last part of the path, "/Value", is the final node you want to return through SelectSingleNode.

 

From that node, you can get its inner text, the value in your case.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Nerseus, you're the best! Worked perfectly. Why would it not say something about this in any of the books that I own :confused: . Thanks again Nerseus :D

 

Here is my new code to find the ip address:

public void ReadXML(string strDocument)
	{
			XmlTextReader xtr = new XmlTextReader(strDocument);
			Console.WriteLine("Inside");
			XmlDocument xd = new XmlDocument();
			Console.WriteLine("Document Loading...");
			xd.Load(xtr);
			Console.WriteLine("Loaded!");
			XmlNode node = xd.SelectSingleNode("/STATUS/SERVERINFO/VARIABLE[@name='ipaddress']/VALUE");
			Console.WriteLine(node.InnerText);
	}

-Sean

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...