how to get the value of an xml document element?


i have an xml document and need to retrieve the value of a particular node. any one can tell me a neat way to retrieve it?

the xml document is as below:
<?xml version="1.0" encoding="UTF-8"?>
   <function name="contact_create">
      <ret_val name="c_id" type="integer">236939</ret_val>

who can tell me how to retrieve that 236939 using c# or

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SwapnilSoftware ArchitectCommented:
Hi viola123,
the best way is to write a code ti Querying XML Data with XQuery method in You can get help from following article for how to do it. 


Dim myXmlDoc As New XmlDocument
        Dim myXmlNode As XmlNode

        myXmlDoc.Load(<give u r xml file location/path here..>)

        myXmlNode = myXmlDoc.SelectSingleNode("//connector_ret/function[ @name='contact_create' ]/ret_val[ @name='c_id']" )
u can get the value using   myXmlNode.InnerText  property..

hope this helps

Nandakumar SakthivelTechnology LeadCommented:

        Dim val As String
        Dim xmldoc As New XmlDocument
        Dim childElement As XmlElement
        childElement = xmldoc.SelectSingleNode("//connector_ret/function/ret_val")
        If (childElement.GetAttribute("name") = "c_id") Then
            val = childElement.InnerText()
        End If

You can use some thing like this

Hope this helps


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

viola123Author Commented:
thanks folks!!

but my xml is actually returned as a string. is there any way to load this xml string into xmldocument object?

myXmlDoc.LoadXML(string variable);
viola123Author Commented:
thanks vinodhsomasekharan :-)

but it does work here, the error msg is as below:

Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 3.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 3.

Source Error:

Line 66:
Line 67:         XmlDocument xmldoc = new XmlDocument();
Line 68:         xmldoc.LoadXml(result);

any comments??

can u post what is the value in result variable?
<?xml version="1.0" encoding="UTF-8"?>
the above line must be the very first line in the xmlstring "result"

ensure that there are no white spaces before or in the first line..

hope it helps u.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.