none
Xpathselectelement returns null reference RRS feed

  • Question

  • Hi,

    I am getting a null reference when trying to get the EventID value in this xml:

            Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(New NameTable())
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/win/2004/08/events/event")
    
            Dim root As XDocument = XDocument.Load("C:\xml.xml")
            Dim res = root.XPathSelectElement("./System/EventID", nsmgr).Value  
    Any ideas what I am doing wrong?


    G.Waters

    Tuesday, December 17, 2019 7:44 PM

Answers

  • If fixed it by adding the namespace to the path:

    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(New NameTable())
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/win/2004/08/events/event")
    
            Dim root As XDocument = XDocument.Load("C:\xml.xml")
            Dim res = root.XPathSelectElement("/ns:Event/ns:System/ns:EventID", nsmgr).Value

    Thanks anyway !!!


    G.Waters

    • Marked as answer by George Waters Tuesday, December 17, 2019 9:16 PM
    Tuesday, December 17, 2019 9:16 PM

All replies

  • Hopefully you are not using the exact xml shown as it's showing dashes like being viewed in a web browser.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 17, 2019 7:52 PM
    Moderator
  • Hi Karen !!!

    This is what the plain xml looks like:

    <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
      <System>
        <Provider Name="Microsoft-Windows-Security-Auditing" Guid="{54849625}" />
        <EventID>4624</EventID>
      </System>
    </Event>


    G.Waters


    Tuesday, December 17, 2019 7:57 PM
  • If fixed it by adding the namespace to the path:

    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(New NameTable())
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/win/2004/08/events/event")
    
            Dim root As XDocument = XDocument.Load("C:\xml.xml")
            Dim res = root.XPathSelectElement("/ns:Event/ns:System/ns:EventID", nsmgr).Value

    Thanks anyway !!!


    G.Waters

    • Marked as answer by George Waters Tuesday, December 17, 2019 9:16 PM
    Tuesday, December 17, 2019 9:16 PM