1114godders
asked on
Select Single Node in VB.Net problems
Hi,
I'm having problems removing nodes from an XML file in vb.net 2003. I took my existing functioning VB6 code and altered this to reference the correct objects etc. However i just keep getting an error;
System.NullReferenceExcept ion: Object reference not set to an instance of an object. I am using the wrong object for oRefNode & oNode?
This is driving me mad an any ideas would be gratefully appreciated.
Thanks,
G
CODE BELOW
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---
Dim mdocNewXmlDoc2 As XmlDocument
Dim oRefNode As XmlNode
Dim oNode As XmlNode
mdocNewXmlDoc2 = New XmlDocument
mdocNewXmlDoc2.Load("C:\Pr ojects\CIS \xmlOut.xm l")
oRefNode = mdocNewXmlDoc2.SelectSingl eNode("/Go vTalkMessa ge/Body/IR envelope/I Rheader")
oNode = mdocNewXmlDoc2.SelectSingl eNode("/Go vTalkMessa ge/Body/IR envelope/I Rheader/IR mark")
Try
If Not oNode.InnerText = vbNull Then
oRefNode.RemoveChild(oNode )
End If
Catch esc As SystemException
MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
Console.WriteLine("******* ********** ********** ********** ********** *")
Console.WriteLine(esc.ToSt ring())
End Try
I'm having problems removing nodes from an XML file in vb.net 2003. I took my existing functioning VB6 code and altered this to reference the correct objects etc. However i just keep getting an error;
System.NullReferenceExcept
This is driving me mad an any ideas would be gratefully appreciated.
Thanks,
G
CODE BELOW
--------------------------
Dim mdocNewXmlDoc2 As XmlDocument
Dim oRefNode As XmlNode
Dim oNode As XmlNode
mdocNewXmlDoc2 = New XmlDocument
mdocNewXmlDoc2.Load("C:\Pr
oRefNode = mdocNewXmlDoc2.SelectSingl
oNode = mdocNewXmlDoc2.SelectSingl
Try
If Not oNode.InnerText = vbNull Then
oRefNode.RemoveChild(oNode
End If
Catch esc As SystemException
MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
Console.WriteLine("*******
Console.WriteLine(esc.ToSt
End Try
Never mind the . in the path. Once your path is correct.
ASKER
Darren,
many thanks for that, however i am still getting an "System.NullReferenceExcep tion: Object reference not set to an instance of an object" on the RemoveChild function, i know i'm not far away from getiting this to work, but it feels like much further at the moment.
And the objectoNode is still null after;
oNode = oRefNode.SelectSingleNode( "/GovTalkM essage/Bod y/IRenvelo pe/IRheade r/IRmark")
mdocNewXmlDoc2 = New XmlDocument
mdocNewXmlDoc2.Load("C:\Pr ojects\CIS \xmlOut.xm l")
oRefNode = mdocNewXmlDoc2.DocumentEle ment
oNode = oRefNode.SelectSingleNode( "/GovTalkM essage/Bod y/IRenvelo pe/IRheade r/IRmark")
Try
oRefNode.RemoveChild(oNode )
Catch esc As SystemException
MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
Console.WriteLine(esc.ToSt ring())
End Try
many thanks for that, however i am still getting an "System.NullReferenceExcep
And the objectoNode is still null after;
oNode = oRefNode.SelectSingleNode(
mdocNewXmlDoc2 = New XmlDocument
mdocNewXmlDoc2.Load("C:\Pr
oRefNode = mdocNewXmlDoc2.DocumentEle
oNode = oRefNode.SelectSingleNode(
Try
oRefNode.RemoveChild(oNode
Catch esc As SystemException
MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
Console.WriteLine(esc.ToSt
End Try
Can you put up you XML file??
ASKER
Sure,
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</Enve lopeVersio n>
<Header>
<MessageDetails>
<Class>IR-CIS-VERIFY</Clas s>
<Qualifier>request</Qualif ier>
<Function>submit</Function >
<CorrelationID/>
<Transformation>XML</Trans formation>
<GatewayTest>1</GatewayTes t>
<GatewayTimestamp/>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>CIS161</SenderID >
<Authentication>
<Method>clear</Method>
<Role>principal</Role>
<Value>testing1</Value>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys>
<Key Type="TaxOfficeNumber">123 </Key>
<Key Type="TaxOfficeReference"> R161</Key>
</Keys>
<ChannelRouting>
<Channel>
<URI>0772</URI>
<Product>HRWorks CIS</Product>
</Channel>
</ChannelRouting>
</GovTalkDetails>
<Body>
<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/CISrequest">
<IRheader>
<Keys>
<Key Type="TaxOfficeNumber">123 </Key>
<Key Type="TaxOfficeReference"> R161</Key>
</Keys>
<PeriodEnd>2008-04-05</Per iodEnd>
<Principal>
<Contact>
<Name>
<Ttl>Mr</Ttl>
<Fore>Brian</Fore>
<Sur>Cooper</Sur>
</Name>
<Email Preferred="yes">brian.coop er@wealden .net</Emai l>
<Telephone Mobile="no" Preferred="no" Type="work">
<Number>0208 3647177</Number>
</Telephone>
<Fax Mobile="no" Preferred="no" Type="work">
<Number>0208 3647181</Number>
</Fax>
</Contact>
</Principal>
<DefaultCurrency>GBP</Defa ultCurrenc y>
<IRmark Type="generic"> </IRmark>
<Sender>Employer</Sender>
</IRheader>
<CISrequest>
<Contractor>
<UTR>9325648154</UTR>
<AOref>123PP87654321</AOre f>
</Contractor>
<Subcontractor>
<Action>verify</Action>
<Type>company</Type>
<TradingName>Plastered</Tr adingName>
<WorksRef>ZA001</WorksRef>
<CRN>NE987654</CRN>
</Subcontractor>
<Declaration>yes</Declarat ion>
</CISrequest>
</IRenvelope>
</Body>
</GovTalkMessage>
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</Enve
<Header>
<MessageDetails>
<Class>IR-CIS-VERIFY</Clas
<Qualifier>request</Qualif
<Function>submit</Function
<CorrelationID/>
<Transformation>XML</Trans
<GatewayTest>1</GatewayTes
<GatewayTimestamp/>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>CIS161</SenderID
<Authentication>
<Method>clear</Method>
<Role>principal</Role>
<Value>testing1</Value>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys>
<Key Type="TaxOfficeNumber">123
<Key Type="TaxOfficeReference">
</Keys>
<ChannelRouting>
<Channel>
<URI>0772</URI>
<Product>HRWorks CIS</Product>
</Channel>
</ChannelRouting>
</GovTalkDetails>
<Body>
<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/CISrequest">
<IRheader>
<Keys>
<Key Type="TaxOfficeNumber">123
<Key Type="TaxOfficeReference">
</Keys>
<PeriodEnd>2008-04-05</Per
<Principal>
<Contact>
<Name>
<Ttl>Mr</Ttl>
<Fore>Brian</Fore>
<Sur>Cooper</Sur>
</Name>
<Email Preferred="yes">brian.coop
<Telephone Mobile="no" Preferred="no" Type="work">
<Number>0208 3647177</Number>
</Telephone>
<Fax Mobile="no" Preferred="no" Type="work">
<Number>0208 3647181</Number>
</Fax>
</Contact>
</Principal>
<DefaultCurrency>GBP</Defa
<IRmark Type="generic"> </IRmark>
<Sender>Employer</Sender>
</IRheader>
<CISrequest>
<Contractor>
<UTR>9325648154</UTR>
<AOref>123PP87654321</AOre
</Contractor>
<Subcontractor>
<Action>verify</Action>
<Type>company</Type>
<TradingName>Plastered</Tr
<WorksRef>ZA001</WorksRef>
<CRN>NE987654</CRN>
</Subcontractor>
<Declaration>yes</Declarat
</CISrequest>
</IRenvelope>
</Body>
</GovTalkMessage>
Hi,
There is nothing wrong with what you are doing to pull out the nodes.
The problem is with the Namespaces. These seem to be causing a problem. If you take them out it works fine.
I'm don't know off the top of my head how to fix this but there is probably some setting or something.
Sorry I can't be of more help.
Darren
Code:
Dim mdocNewXmlDoc2 As New XmlDocument
Dim oRootNode As XmlNode
Dim oNode As XmlNode
Try
mdocNewXmlDoc2.Load("C:\Pr ojects\CIS \xmlOut.xm l")
oRootNode = mdocNewXmlDoc2.DocumentEle ment()
oNode = oRootNode.SelectSingleNode ("/GovTalk Message/Bo dy/IRenvel ope/IRhead er/IRmark" )
If Not oNode Is Nothing Then
oRootNode.RemoveChild(oNod e)
End If
Catch esc As SystemException
MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
Console.WriteLine("******* ********** ********** ********** ********** *")
Console.WriteLine(esc.ToSt ring())
End Try
There is nothing wrong with what you are doing to pull out the nodes.
The problem is with the Namespaces. These seem to be causing a problem. If you take them out it works fine.
I'm don't know off the top of my head how to fix this but there is probably some setting or something.
Sorry I can't be of more help.
Darren
Code:
Dim mdocNewXmlDoc2 As New XmlDocument
Dim oRootNode As XmlNode
Dim oNode As XmlNode
Try
mdocNewXmlDoc2.Load("C:\Pr
oRootNode = mdocNewXmlDoc2.DocumentEle
oNode = oRootNode.SelectSingleNode
If Not oNode Is Nothing Then
oRootNode.RemoveChild(oNod
End If
Catch esc As SystemException
MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
Console.WriteLine("*******
Console.WriteLine(esc.ToSt
End Try
ASKER
Darren,
Many thanks for that, by the Namespaces do you mean the ones in VB.Net? If i remove the namespaces won't the code just not run at all, im a bit confused here?
G
Many thanks for that, by the Namespaces do you mean the ones in VB.Net? If i remove the namespaces won't the code just not run at all, im a bit confused here?
G
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very strange, will need to think about this one a while, but i'll give you points for at least figuring out why it wasn't working.
I know it's really wierd
Have a look here
http://pluralsight.com/blogs/keith/archive/2005/10/19/15714.aspx
Have a look here
http://pluralsight.com/blogs/keith/archive/2005/10/19/15714.aspx
That article shows how to strip out the xmlns using regular expressions further down the post.
Probably better off reading the entire article comments and all.
Darren
Probably better off reading the entire article comments and all.
Darren
Dim oNodeRoot, oNode As XmlNode
' Load the root node with the entire document.
oNodeRoot = oXMLDOMDoc.DocumentElement
' Retrieve the required node using xPath.
oNode = oNodeRoot.SelectSingleNode
Darren