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

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

Hi

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"?>
<login-response
  xsi:schemaLocation="http://xml-schemas.bcbsil.com/b2b/b2b.xsd b2b/b2b.xsd"
  xmlns="http://xml-schemas.bcbsil.com/b2b/b2b.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <response-header
    status="OK" audit-id="LOGIN"/>
    <login-data session-token=";jsessionid=0000nVG0vvqKqLQwaYLuNRJnnku:12vbdkshu"/>
</login-response>

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>
  <author>
    <first-name>Jane</first-name>
    <last-name>Austen</last-name>
  </author>
  <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
  <title>The Handmaid's Tale</title>
  <author>
    <first-name>Margaret</first-name>
    <last-name>Atwood</last-name>
  </author>
  <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
  <title>Emma</title>
  <author>
    <first-name>Jane</first-name>
    <last-name>Austen</last-name>
  </author>
  <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
  <title>Sense and Sensibility</title>
  <author>
    <first-name>Jane</first-name>
    <last-name>Austen</last-name>
  </author>
  <price>19.95</price>
  </book>
</bookstore>


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


XPathIterator xit = XPathNavigator1.Select("/bookstore/book/@bk:ISBN");
xit.MoveNext();
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
hi,

try this code:

' Load the XML file.
Dim xml_doc As New Xml.XmlDocument
xml_doc.Load(txtFile.Text)
 
' Get the desired children.
Dim child_nodes As XmlNodeList = _
  xml_doc.GetElementsByTagName(txtTagName.Text)
 
' 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
Read:
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;
  }

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

http://vbnetsample.blogspot.com/2007/07/read-xml-content-by-xmldocument.html


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
m_xmld.Load("YourPath\test.xml")

'Show all data in your xml
MessageBox.Show(m_xmld.OuterXml)


'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)
Console.Write(vbCrLf)
Next

TRy this and let me know.