VB.NET - How to get the value of the particular node from xml

Asked By abinav shankar on 15-Dec-11 11:54 AM


I tried getting the value of a single node from xmldocument but it is not getting loaded i can see the innerxml but i cannot get the particular node and I need jsessionid from this response how to do it

<?xml version="1.0" encoding="UTF-8"?>
  xsi:schemaLocation="http://xml-schemas.bcbsil.com/b2b/b2b.xsd b2b/b2b.xsd"
    status="OK" audit-id="LOGIN"/>
    <login-data session-token=";jsessionid=0000nVG0vvqKqLQwaYLuNRJnnku:12vbdkshu"/>

I need the jsessionId from this xml pl help me

Suchit shah replied to abinav shankar on 15-Dec-11 12:06 PM
You can get it like below example one 

say my xml file look like this

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
  <title>Pride And Prejudice</title>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
  <title>The Handmaid's Tale</title>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
  <title>Sense and Sensibility</title>

and i want to get the node then need to do it like below one

XPathIterator xit = XPathNavigator1.Select("/bookstore/book/@bk:ISBN");
String value = xit.Current.Value;

Hope you get the Idea and it helps
kalpana aparnathi replied to abinav shankar on 15-Dec-11 12:17 PM

try this code:

' Load the XML file.
Dim xml_doc As New Xml.XmlDocument
' Get the desired children.
Dim child_nodes As XmlNodeList = _
' Process the children.
Dim txt As String = ""
For Each child As XmlNode In child_nodes
  txt &= _
    child.Attributes("Name").InnerXml & " = " & _
    child.InnerText & vbCrLf
Next child
txtResults.Text = txt
dipa ahuja replied to abinav shankar on 15-Dec-11 12:32 PM
Try this way
XDocument nodes = XDocument.Load("yourfilename.xml");

foreach(var node in node.Descendants("parentAttribute"))
string at1 = coordinate.Attribute("atrubute1").Value;

string at2 = coordinate.Element("atribute2").Value;
string at3 = coordinate.Element("atribute3").Value;

// do whatever you want to do with those items of information now

Riley K replied to abinav shankar on 15-Dec-11 08:10 PM

Here is the code

XmlNodeList elemList = doc.GetElementsByTagName("login-data session-token");
  for (int i = 0; i < elemList.Count; i++)
    string attrVal = elemList[i].Attributes["jsessionid"].Value;

Jitendra Faye replied to abinav shankar on 15-Dec-11 11:07 PM


This sample show, How to read xml content by use XmlDocument namespace.

You can see this http://4.bp.blogspot.com/_3wnvoEJaw_Y/RojJAFSVwyI/AAAAAAAAAAs/i5sEGQ6nYqw/s1600-h/sampleXML.PNGfor xml sample content.

Sample Code

Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode

'Create the XML Document
m_xmld = New XmlDocument()

'Load the Xml file

'Show all data in your xml

'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/family/name")

'Loop through the nodes
For Each m_node In m_nodelist
'Get the Gender Attribute Value
Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value

'Get the firstName Element Value
Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText

'Get the lastName Element Value
Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText

'Write Result to the Console
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)

TRy this and let me know.