Solved

how to get the value of an xml document element?

Posted on 2006-07-11
9
1,316 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
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17080775
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now