XML Read specific Node
Asked Answered
L

4

0

I want to read an specific xml node and its value for example

<customers>
<name>John</name>
<lastname>fetcher</lastname>
</customer>

and my code behind should be some thing like this (I don't know how it should be though):

Response.Write(xml.Node["name"].Value) 

As I said it is just an example because I don't know how to do it.

Lauro answered 11/12, 2008 at 17:43 Comment(0)
I
3

The most basic answer:
Assuming "xml" is an XMLDocument, XMLNodeList, XMLNode, etc...

Response.Write(xml.SelectSingleNode("//name").innerText)
Isosteric answered 11/12, 2008 at 17:50 Comment(0)
A
3

Which version of .NET are you using? If you're using .NET 3.5 and can use LINQ to XML, it's as simple as:

document.Descendant("name").Value

(except with some error handling!) If you're stuk with the DOM API, you might want:

document.SelectSingleNode("//name").InnerText

Note that this hasn't shown anything about how you'd read the XML in the first place - if you need help with that bit, please give more detail in the question.

Aft answered 11/12, 2008 at 17:48 Comment(1)
Thanks, Actually what you suggested is enough for me for now :)Lauro
I
3

The most basic answer:
Assuming "xml" is an XMLDocument, XMLNodeList, XMLNode, etc...

Response.Write(xml.SelectSingleNode("//name").innerText)
Isosteric answered 11/12, 2008 at 17:50 Comment(0)
O
2

If using earlier versions of the .Net framework, take a look at the XMLDocument class first as this is what you'd load the XML string into. Subclasses like XMLElement and XMLNode are also useful for doing some of this work.

Orthodontia answered 11/12, 2008 at 17:52 Comment(0)
D
1

haven't tried testing it but should point you in the right direction anyway

 'Create the XML Document
 Dim l_xmld As XmlDocument
'Create the XML Node
        Dim l_node As XmlNode

            l_xmld = New XmlDocument

            'Load the Xml file
            l_xmld.LoadXml("XML Filename as String")

            'get the attributes
            l_node = l_xmld.SelectSingleNode("/customers/name")

           Response.Write(l_node.InnerText)
Daiseydaisi answered 11/12, 2008 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.