Solved

how to get the value of an xml document element?

Posted on 2006-07-11
9
1,322 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 55
asp web application 3 43
Can Angular be used with classic asp.net? 3 34
asp.net figure out error 3 26
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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…

730 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