Reading XML Node

I want to read a specific xml node and this value, for example

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

      

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

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

      

blah blah. As I said, this is just an example because I don't know how to do it. So could you please help me.

Thank.

Respectfully..

0


source to share


4 answers



Simplest answer: Assuming "xml" is XMLDocument, XMLNodeList, XMLNode, etc.



Response.Write(xml.SelectSingleNode("//name").innerText)

      

+3


source


What version of .NET are you using? If you are using .NET 3.5 and can use LINQ to XML, this is simple:

document.Descendant("name").Value

      

(minus some bugs!) If you are working with the DOM API, you may need:



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

      

Note that this didn't show anything about how you read the XML in the first place - if you need help with this bit, please provide more details in the question.

+3


source


If you are using earlier versions of the .Net framework, consider XMLDocument first as this is what you will load the XML string. Subclasses, such as XMLElement and XMLNode , are also useful for doing some of this work.

+2


source


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

 '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)

      

+1


source







All Articles