Results 1 to 18 of 18

Thread: [RESOLVED] Reading XML -> Object reference not set...

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Reading XML -> Object reference not set...

    Hi,

    I'm trying to get the AppVersion from this app.xml from Excel 2010, but it results in "Object reference not set to an instance of an object."

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
    	<Application>Microsoft Excel</Application>
    	<DocSecurity>0</DocSecurity>
    	<ScaleCrop>false</ScaleCrop>
    	<HeadingPairs><vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>Werkbladen</vt:lpstr></vt:variant><vt:variant><vt:i4>3</vt:i4></vt:variant></vt:vector></HeadingPairs>
    	<TitlesOfParts><vt:vector size="3" baseType="lpstr"><vt:lpstr>Blad1</vt:lpstr><vt:lpstr>Blad2</vt:lpstr><vt:lpstr>Blad3</vt:lpstr></vt:vector></TitlesOfParts>
    	<Company>Home</Company>
    	<LinksUpToDate>false</LinksUpToDate>
    	<SharedDoc>false</SharedDoc>
    	<HyperlinksChanged>false</HyperlinksChanged>
    	<AppVersion>14.0300</AppVersion>
    </Properties>
    My (simple) code:

    Code:
    Dim docversion = XElement.Load(Application.StartupPath & "\app.xml")
    MsgBox(docversion.Descendants("AppVersion").Nodes(0).ToString)
    I have used it before in my own custom created xml files without any issues. Don't understand why this doesn't work.

    Any help much appreciated.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Reading XML -> Object reference not set...

    The AppVersion node doesn't have any nodes so the .Nodes(0) is probably failing (returning Nothing).

    -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
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: Reading XML -> Object reference not set...

    It's slightly easier to write if you use XML Literals.

    Code:
    Dim docversion = System.XML.Linq.XMLDocument.Load(Application.StartupPath & "\app.xml")
    Dim app_Verison = docversion.<AppVersion>.Value

  4. #4

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Reading XML -> Object reference not set...

    Thanks guys, but same story. Anyone else with some tips or a solution?
    (the file is normally one line, I broke it up for readability)


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  5. #5
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Reading XML -> Object reference not set...

    Hi,

    The bit that has been missed here is that the XML file has a Declared Namespace due to this Attribute associated with the Properties Node:-

    XML Code:
    1. xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"

    You therefore need to use a Namespace Manager to access that Namespace and read the nodes. i.e:-

    vb.net Code:
    1. Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    2.   Dim myXMLDoc As New XmlDocument
    3.   myXMLDoc.Load(IO.Path.Combine(Application.StartupPath, "XMLFile1.xml"))
    4.  
    5.   'Define a Namespace Manager for the XML file and then Declare that Namespace
    6.   Dim myNSManager = New XmlNamespaceManager(myXMLDoc.NameTable)
    7.   myNSManager.AddNamespace("myNameSpace", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties")
    8.  
    9.   MsgBox(myXMLDoc.DocumentElement.SelectSingleNode("myNameSpace:AppVersion", myNSManager).InnerText)
    10. End Sub

    Hope that helps.

    Cheers,

    Ian

  6. #6

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Reading XML -> Object reference not set...

    Hi Ian. Thanks for making things more clear. +REP.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  7. #7
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: [RESOLVED] Reading XML -> Object reference not set...

    Or you could import that as the default xmlnamespace.

    At the top the file
    Code:
    Imports <xmlns ="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">

    Code:
    Dim d = <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
    	<Application>Microsoft Excel</Application>
    	<DocSecurity>0</DocSecurity>
    	<ScaleCrop>false</ScaleCrop>
    	<HeadingPairs><vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>Werkbladen</vt:lpstr></vt:variant><vt:variant><vt:i4>3</vt:i4></vt:variant></vt:vector></HeadingPairs>
    	<TitlesOfParts><vt:vector size="3" baseType="lpstr"><vt:lpstr>Blad1</vt:lpstr><vt:lpstr>Blad2</vt:lpstr><vt:lpstr>Blad3</vt:lpstr></vt:vector></TitlesOfParts>
    	<Company>Home</Company>
    	<LinksUpToDate>false</LinksUpToDate>
    	<SharedDoc>false</SharedDoc>
    	<HyperlinksChanged>false</HyperlinksChanged>
    	<AppVersion>14.0300</AppVersion>
    </Properties>
    
    Dim v = d.<Properties>.<AppVersion>.Value
    Now correctly gets the value 14.0300 .

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Reading XML -> Object reference not set...

    Quote Originally Posted by IanRyder View Post
    Hi,

    The bit that has been missed here is that the XML file has a Declared Namespace due to this Attribute associated with the Properties Node:-

    XML Code:
    1. xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"

    You therefore need to use a Namespace Manager to access that Namespace and read the nodes. i.e:-

    vb.net Code:
    1. Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    2.   Dim myXMLDoc As New XmlDocument
    3.   myXMLDoc.Load(IO.Path.Combine(Application.StartupPath, "XMLFile1.xml"))
    4.  
    5.   'Define a Namespace Manager for the XML file and then Declare that Namespace
    6.   Dim myNSManager = New XmlNamespaceManager(myXMLDoc.NameTable)
    7.   myNSManager.AddNamespace("myNameSpace", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties")
    8.  
    9.   MsgBox(myXMLDoc.DocumentElement.SelectSingleNode("myNameSpace:AppVersion", myNSManager).InnerText)
    10. End Sub

    Hope that helps.

    Cheers,

    Ian
    Even though the node in question ISN'T part of the namespace? ... Oooooh.... it's because it's in the default namespace... duh...

    -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??? *

  9. #9
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Reading XML -> Object reference not set...

    Quote Originally Posted by techgnome View Post
    Even though the node in question ISN'T part of the namespace? ... Oooooh.... it's because it's in the default namespace... duh...

    -tg
    Eh, What the heck does that mean?

    If you would like to make a rational comment on something that I have misunderstood or misquoted then please do make that comment rather than posting some inane words.

    Ian

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Reading XML -> Object reference not set...

    The node that hte OP was after wasn't part of the namespace.... so I was wondering how adding a name space "fixed" it... unless it's because the node in question was part of the default namespace. No need to make a federal case out of 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??? *

  11. #11
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: [RESOLVED] Reading XML -> Object reference not set...

    You are mistaken.

    The node in question WAS part of the declared namespace and NOT in the default namespace. Try it for yourself and by all means prove me wrong so that we call all learn something new.

    Cheers,

    Ian

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Reading XML -> Object reference not set...

    maybe I'm wrong but I read this:
    Code:
    xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
    As two namespaces... the first being the default namespace... the second as a named one of vt....

    The AppVersion node, having no namespace predecessor on it falls into the default namespace:
    Code:
    <AppVersion>14.0300</AppVersion>
    Unlike the child elements of the HeadingPairs node... which are part of the vt namespace...
    Code:
    <HeadingPairs><vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>Werkbladen</vt:lpstr></vt:variant><vt:variant><vt:i4>3</vt:i4></vt:variant></vt:vector></HeadingPairs>
    So... where did I go wrong? The OP was after the AppVersion... which should have been in the default namespace.


    -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??? *

  13. #13
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: [RESOLVED] Reading XML -> Object reference not set...

    Hi techgnome,

    Unlike the child elements of the HeadingPairs node... which are part of the vt namespace...

    XML Code etc...

    So... where did I go wrong? The OP was after the AppVersion... which should have been in the default namespace.
    On that point, I totally agree with you. However, and even my own comments were probably not specific enough, this bit of XML:-

    XML Code:
    1. <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" etc...>

    effectively changed the Default Namespace to this string which then needs to be qualified to access the Child nodes.

    I hope this helps to clear up the confusion.

    Cheers,

    Ian

    BTW, I particularly liked the trick that AdamPanic2013 used in Post #7 to Import the Namespace. That was one that I did not know until now.

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Reading XML -> Object reference not set...

    Right... and that's what occurred to me as I was writing my response... hence "the ooooh..... duh" ... what you got in my response was an unfiltered stream of consciousness...

    Yeah, that trick was a new one to me too... but given that you can now have XML literals... shouldn't have been too surprising... although it gives new meaning to importing a namespace!

    -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??? *

  15. #15
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] Reading XML -> Object reference not set...

    New trick also, fails though here n:?

    Code:
    Dim v = d.<Properties>.<AppVersion>.Value
    works

    Code:
    d.<AppVersion>.Value
    My Github - 1d3nt

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Reading XML -> Object reference not set...

    Quote Originally Posted by IanRyder View Post
    Hi techgnome,



    On that point, I totally agree with you. However, and even my own comments were probably not specific enough, this bit of XML:-

    XML Code:
    1. <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" etc...>

    effectively changed the Default Namespace to this string which then needs to be qualified to access the Child nodes.

    I hope this helps to clear up the confusion.

    Cheers,

    Ian

    BTW, I particularly liked the trick that AdamPanic2013 used in Post #7 to Import the Namespace. That was one that I did not know until now.
    Ooooooh... I had to read that three times before I caught onto the namespace in the node... ok... what you said about it not being in the default namespace makes TOTAL sense now.... "If everyone would turn to page 42...."


    Quote Originally Posted by ident View Post
    New trick also, fails though here n:?

    Code:
    Dim v = d.<Properties>.<AppVersion>.Value
    works

    Code:
    d.<AppVersion>.Value
    Right...because Properties is the root document node... represented by d...
    d doesn't hold the Properties node... it IS the Properties node.


    -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??? *

  17. #17
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: [RESOLVED] Reading XML -> Object reference not set...

    Unfortunately that's not correct. It would be true if d was a XMLElement but because of the following at the start.
    Code:
     <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    changes d to be a XDocument.
    The root of a XDocument is the document itself, there could (and can) be extra nodes after the properties closing node.

    Hence the inclusion of the <Properties>.

    Edit: In mine I've been using the XML given in the original post for d.
    Last edited by AdamPanic2013; Nov 11th, 2013 at 11:21 AM.

  18. #18
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] Reading XML -> Object reference not set...

    never said it didnt, it was posted
    My Github - 1d3nt

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