|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Best way to get the value of one particular node of an XML stringwhere 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 Scott M. Lyon wrote:
Show quoteHide quote > I'm trying to figure out the best way (considering there could be instances You can do what you want using XmlDocument.SelectNodes or > 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 > > 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 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
Why use Events?
[Datable] change column datatype ownerdraw menu, help appreciated File Path (Application Path) Custom collections Crystal Report Database Change Update Command with Parameter.... No one Find Directory created by me... DataGrid LostFocus does not seem right Access parent form (protected friend/friend) variable in child form |
|||||||||||||||||||||||