views:

1141

answers:

4

hi,

I want to read an specific xml node and it's 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 tho :))

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

blah blah. As I said It's just an example cuz I don't know how to do. So Could you help me please.

Thanks.

Regards..

+2  A: 

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.

Jon Skeet
Thanks, Actually what you suggested is enough for me for now :)
Braveyard
+2  A: 

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

Response.Write(xml.SelectSingleNode("//name").innerText)
Rich
+2  A: 

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.

JB King
+1  A: 

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)
Phil Corcoran