Groups | Blog | Home
all groups > dotnet xml > april 2005 >

dotnet xml : Best way to get the value of one particular node of an XML string


Scott M. Lyon
4/14/2005 8:12:14 AM
I'm trying to figure out the best way (considering there could be instances
where I get a lot of data in this XML, and I want to minimize any slowdowns)
to extract the value of one particular node from an XML string (not saved as
a file, but passed as a string from another module).


For example, let's assume I get back XML in a string that looks like this:

<Commands>
<cmd name="1">Value 1</cmd>
<cmd name="2">Value 2</cmd>
<cmd name="3">Value 3</cmd>
</Commands>


I need an efficient way that I can (for example) return the value from the
cmd tag with name attribute "2" (so for this example, to return "Value 2").


What is going to be the best way to do this? It was suggested (here) that I
use the XmlTextReader, but I'm not totally sure how that would work.


Can anyone help?


Ideally I'd like a function (VB.NET) that takes the xml as one parameter (as
a string), and the cmd "name" as another parameter (also as a string), and
returns a string containing the value.


Thanks!
-Scott

Scott M. Lyon
4/14/2005 11:06:10 AM
What if it were one level more complicated?

For example, given this data:

<Commands>
<cmd name="1">
<item name="result">Complete</item>
</cmd>
<cmd name="2">
<item name="result">Returned</item>
<item name="id">12345</item>
</cmd>
<cmd name="3">
<item name="result">Tested</item>
</cmd>
</Commands>

I need a function that takes the following parameters:
cmd name value
item name value


For example, in one case, I want to get the data from cmd name "2", and item
"result". Another time it may be cmd name "2" and item "id".


Ideas?


Thanks!
-Scott

[quoted text, click to view]

Martin Honnen
4/14/2005 4:18:26 PM


[quoted text, click to view]

Here is a C# method:

public static string GetValue (string xmlMarkup, string elementName,
string attributeName, string attributeValue) {
XmlTextReader xmlReader = new XmlTextReader(new
StringReader(xmlMarkup));
string value = null;
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name
== elementName) {
if (xmlReader.GetAttribute(attributeName) == attributeValue) {
value = xmlReader.ReadElementString();
break;
}
}
}
xmlReader.Close();
return value;
}

here are two example calls:

string exampleMarkup = @"<Commands>
<cmd name=""1"">Value 1</cmd>
<cmd name=""2"">Value 2</cmd>
<cmd name=""3"">Value 3</cmd>
</Commands>";
string valueFound = GetValue(exampleMarkup, "cmd", "name", "2");
Console.WriteLine("Found value \"{0}\".", valueFound);
valueFound = GetValue(exampleMarkup, "cmd", "name", "5");
Console.WriteLine("Found value \"{0}\".", valueFound);


--

Martin Honnen --- MVP XML
Martin Honnen
4/14/2005 6:28:40 PM


[quoted text, click to view]

Check out the XmlTextReader API then, you simply pull in nodes calling
the Read method or more specialized methods and can check the current
node type, node name as my example showed. You only have the implement
the logic then to check the attribute value of <cmd> elements and if you
find what you are looking for to implement the next lookup for the
<item> element with the proper attribute value.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlTextReaderClassTopic.asp>


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button