Solved

how to get the value of an xml document element?

Posted on 2006-07-11
9
1,321 Views
Last Modified: 2013-11-19
hi,

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"?>
<connector_ret>
   <function name="contact_create">
      <ret_val name="c_id" type="integer">236939</ret_val>
   </function>
</connector_ret>


who can tell me how to retrieve that 236939 using c# or vb.net?

thanks
viola123
0
Comment
Question by:viola123
9 Comments
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 17080664
Hi viola123,
the best way is to write a code ti Querying XML Data with XQuery method in asp.net. You can get help from following article for how to do it.

http://aspnet.4guysfromrolla.com/articles/071603-1.aspx 


Regards,
NetSwap
0
 
LVL 5

Assisted Solution

by:vinodhsomasekharan
vinodhsomasekharan earned 350 total points
ID: 17080680
Hai,

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']" )
       
      myXmlNode.InnerText
u can get the value using   myXmlNode.InnerText  property..


hope this helps

Regards
Vinu
0
 
LVL 6

Accepted Solution

by:
Nandakumar Sakthivel earned 150 total points
ID: 17080682
Hi,

        Dim val As String
        Dim xmldoc As New XmlDocument
        xmldoc.Load("example.xml")
        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

Nanda
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17080775
0
 
LVL 6

Author Comment

by:viola123
ID: 17080941
thanks folks!!

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

viola123
0
 
LVL 5

Expert Comment

by:vinodhsomasekharan
ID: 17081027
myXmlDoc.LoadXML(string variable);
0
 
LVL 6

Author Comment

by:viola123
ID: 17081063
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??

thanks
0
 
LVL 5

Expert Comment

by:vinodhsomasekharan
ID: 17081725
can u post what is the value in result variable?
0
 
LVL 5

Expert Comment

by:vinodhsomasekharan
ID: 17081763
<?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.

Regards
Vinodh
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question