-
Sep 6th, 2023, 09:05 AM
#1
Thread Starter
New Member
Problem retrieving XML node
I am starting with some new code which interacts with a website, logs in & gets various XML responses.. I am struggling to retrieve the session_id field from the following message.
HTML Code:
<?xml version="1.0"?>
<HamQTH version="2.7" xmlns="https://www.hamqth.com">
<session>
<session_id>09b0ae90050be03c452ad235a1f2915ad684393c</session_id>
</session>
</HamQTH>
Code as follows;
Code:
Dim xmlDoc = New XmlDocument
Dim xmlNode As Xml.XmlNode
xmlDoc.LoadXml(strXMLResponse)
Dim xpath As String = "/HamQTH[@xmlns=""https: //www.hamqth.com""]/session/session_id/text()"
xmlNode = xmlDoc.SelectSingleNode(xpath)
If XmlNode Is Nothing Then
MsgBox("Node not found")
Else
MsgBox(strSessionId)
End If
the xpath was generated using the message & the Google xmltoolbox ... I just get node not found
-
Sep 6th, 2023, 10:06 AM
#2
Re: Problem retrieving XML node
Drop the "/text()" on the end of your xpath ... SelectSingleNode returns a NODE ... but the xpath you're using is designed to return the text of the node ... which SelectSingleNode will not do... so select the node, then get the text from it.
-tg
-
Sep 6th, 2023, 10:07 AM
#3
Re: Problem retrieving XML node
The issue is that the namespace has a colon in it.
If you don't need it, then just remove it, then do your query.
Code:
Dim literal = "<?xml version=""1.0""?><HamQTH version=""2.7"" xmlns=""https://www.hamqth.com""><session><session_id>09b0ae90050be03c452ad235a1f2915ad684393c</session_id></session></HamQTH>".Replace("xmlns=""https://www.hamqth.com""", String.Empty)
Dim parsedXml = XDocument.Parse(literal)
Dim sessionIdElement = parsedXml.Root.Element("session").Element("session_id")
Console.WriteLine(sessionIdElement)
Fiddle: https://dotnetfiddle.net/8PPnbw
-
Sep 6th, 2023, 11:37 AM
#4
Thread Starter
New Member
Re: Problem retrieving XML node
Sorry, this made no difference. initially I removed the /text(), then I tried removing the colon... I am not generating the XML so have no control over its format
Clive
-
Sep 6th, 2023, 01:25 PM
#5
Re: Problem retrieving XML node
Try this,
Code:
'
' add this to the beginning of your code
' Imports <xmlns="https://www.hamqth.com">
'
Dim xe As XElement
xe = XElement.Load(strXMLResponse)
Dim sidEL As XElement = xe.<session>.<session_id>.FirstOrDefault
Dim sessionID As String = sidEL.Value
The imports location
Code:
Imports <xmlns="https://www.hamqth.com">
Public Class Form1
Last edited by dbasnett; Sep 6th, 2023 at 02:55 PM.
-
Sep 7th, 2023, 02:30 AM
#6
Thread Starter
New Member
Re: Problem retrieving XML node
this causes a System.Io.IOexception .... change the load to a parse and worked.
Clive
Last edited by g4naq; Sep 7th, 2023 at 03:26 AM.
-
Sep 7th, 2023, 10:44 AM
#7
Re: Problem retrieving XML node
 Originally Posted by g4naq
this causes a System.Io.IOexception .... change the load to a parse and worked.
Clive
Is strXMLResponse a valid URI? I'm guessing it is a string. Glad it worked out.
-
Sep 7th, 2023, 11:02 AM
#8
Thread Starter
New Member
Re: Problem retrieving XML node
yes, its the complete XML string.. thanks for your help
Clive
-
Sep 8th, 2023, 09:41 AM
#9
Re: Problem retrieving XML node
Why not this: ?
I accidently started it in C# but you can convert easily
Code:
//
using System.IO;
using System.Xml.Serialization;
///
XmlSerializer srl = new XmlSerializer(typeof(HamQTH));
HamQTH schm;
using (var sr = new StreamReader("C:\\zxzxzxzxzxz11\\1.txt"))
{
schm = (HamQTH)srl.Deserialize(sr);
}
string session = schm.session.session_id;
//Classes:
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "https://www.hamqth.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "https://www.hamqth.com", IsNullable = false)]
public partial class HamQTH
{
private HamQTHSession sessionField;
private decimal versionField;
/// <remarks/>
public HamQTHSession session
{
get
{
return this.sessionField;
}
set
{
this.sessionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "https://www.hamqth.com")]
public partial class HamQTHSession
{
private string session_idField;
/// <remarks/>
public string session_id
{
get
{
return this.session_idField;
}
set
{
this.session_idField = value;
}
}
}
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|