473,396 Members | 2,050 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,396 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">
<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 an efficient way that I can (for example) return the value from the
cmd tag with name attribute "2", and then the item tag below that with the
name attribute "result" (in other words, for this case giving the string
"Returned".
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
the item "name" as the third parameter, and returns a string containing the
value.
Thanks!
-Scott
Nov 21 '05 #1
2 1470
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">
<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 an efficient way that I can (for example) return the value from the
cmd tag with name attribute "2", and then the item tag below that with the
name attribute "result" (in other words, for this case giving the string
"Returned".
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
the item "name" as the third parameter, and returns a string containing the
value.
Thanks!
-Scott


You can do what you want using XmlDocument.SelectNodes or
XmlDocument.SelectSingNode.

You need to know XPath to do what you want but there is good help and
examples if you look under "XPath [MS XML], examples". I think the path
you need (and I warn you I'm not well practiced in xpath) is 'cmd[@name
= "3"' That says give me all the nodes who name is cmd and has an
attribute of name that equals 3.

Good luck.
Chris
Nov 21 '05 #2
Function GetVal(ByVal xml As String, ByVal cmdname As String, ByVal cmditem
As String) As String
Dim myDoc As New Xml.XmlDocument
myDoc.LoadXml(xml)
For Each Node As Xml.XmlNode In myDoc.FirstChild.ChildNodes
For Each a As Xml.XmlAttribute In Node.Attributes
If (a.Value = cmdname) Then
For Each cNode As Xml.XmlNode In Node.ChildNodes
For Each ca As Xml.XmlAttribute In cNode.Attributes
If (ca.Value = cmditem) Then
GetVal = cNode.InnerText
End If
Next
Next
End If
Next
Next
End Function

Chris's method is definitely cleaner, but might take some time to learn
XPath and it's syntax. Above is a function that will return the text of the
node that is under cmd with name cmdname, and the node that has the name
cmditem.

GetVal(xmlhere, "2", "result") returns "Returned", and GetVal(xmlhere, "2",
"id") returns "12345"

--
Damian

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #3

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",...
3
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...
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). ...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.