473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
Nov 12 '05 #1
3 1851


Scott M. Lyon wrote:
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.


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
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
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

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...


Scott M. Lyon wrote:
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.


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
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


Scott M. Lyon wrote:
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


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
http://JavaScript.FAQTs.com/
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
2
by: Robert W. | last post by:
In a posting earlier this year I found a simple approach to convert a string to a particular Enum value. The one line solution looked like this: MyEnum ConvertedString = (MyEnum)...
1
by: Dica | last post by:
hi all first off, i'm not trying to cross post, but couldn't find this newsgroup earlier (got here from a recommendation on microsoft.public.vb, where i originally posted this question). ...
2
by: Scott M. Lyon | last post by:
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...
5
by: patrin | last post by:
Hi All, given the source document: <?xml version="1.0" encoding="UTF-8"?> <root> <child> <test id="1" name="first child"/> </child> <child>
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
17
by: =?Utf-8?B?VGVycmFuY2U=?= | last post by:
Hello, I was wondering if someone can give me a few pointers in catching an exception correctly or at least making the code a little more elegant than what I have. I have a rss reader that I built...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.