|
-
Nov 8th, 2013, 02:13 PM
#1
Thread Starter
PowerPoster
[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.
-
Nov 8th, 2013, 03:10 PM
#2
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
-
Nov 8th, 2013, 10:40 PM
#3
Lively Member
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
-
Nov 9th, 2013, 03:29 AM
#4
Thread Starter
PowerPoster
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)
-
Nov 9th, 2013, 04:18 AM
#5
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:
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:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim myXMLDoc As New XmlDocument
myXMLDoc.Load(IO.Path.Combine(Application.StartupPath, "XMLFile1.xml"))
'Define a Namespace Manager for the XML file and then Declare that Namespace
Dim myNSManager = New XmlNamespaceManager(myXMLDoc.NameTable)
myNSManager.AddNamespace("myNameSpace", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties")
MsgBox(myXMLDoc.DocumentElement.SelectSingleNode("myNameSpace:AppVersion", myNSManager).InnerText)
End Sub
Hope that helps.
Cheers,
Ian
-
Nov 9th, 2013, 05:20 AM
#6
Thread Starter
PowerPoster
Re: Reading XML -> Object reference not set...
Hi Ian. Thanks for making things more clear. +REP.
-
Nov 10th, 2013, 04:14 AM
#7
Lively Member
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 .
-
Nov 10th, 2013, 07:52 AM
#8
Re: Reading XML -> Object reference not set...
 Originally Posted by IanRyder
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:
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:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim myXMLDoc As New XmlDocument myXMLDoc.Load(IO.Path.Combine(Application.StartupPath, "XMLFile1.xml")) 'Define a Namespace Manager for the XML file and then Declare that Namespace Dim myNSManager = New XmlNamespaceManager(myXMLDoc.NameTable) myNSManager.AddNamespace("myNameSpace", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties") MsgBox(myXMLDoc.DocumentElement.SelectSingleNode("myNameSpace:AppVersion", myNSManager).InnerText) 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
-
Nov 10th, 2013, 08:51 AM
#9
Re: Reading XML -> Object reference not set...
 Originally Posted by techgnome
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
-
Nov 10th, 2013, 12:21 PM
#10
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
-
Nov 10th, 2013, 11:37 PM
#11
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
-
Nov 11th, 2013, 06:48 AM
#12
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
-
Nov 11th, 2013, 09:01 AM
#13
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:
<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.
-
Nov 11th, 2013, 09:34 AM
#14
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
-
Nov 11th, 2013, 09:48 AM
#15
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
-
Nov 11th, 2013, 10:39 AM
#16
Re: [RESOLVED] Reading XML -> Object reference not set...
 Originally Posted by IanRyder
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:
<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...."
 Originally Posted by ident
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
-
Nov 11th, 2013, 11:12 AM
#17
Lively Member
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.
-
Nov 11th, 2013, 05:18 PM
#18
Re: [RESOLVED] Reading XML -> Object reference not set...
never said it didnt, it was posted
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
|