Results 1 to 9 of 9

Thread: Problem retrieving XML node

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    5

    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

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,385

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,501

    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
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    5

    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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,687

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    5

    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.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,687

    Re: Problem retrieving XML node

    Quote Originally Posted by g4naq View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    5

    Re: Problem retrieving XML node

    yes, its the complete XML string.. thanks for your help
    Clive

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,554

    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
  •  



Click Here to Expand Forum to Full Width