Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select Single Node in VB.Net problems

Posted on 2007-03-29
12
Medium Priority
?
633 Views
Last Modified: 2008-01-09
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.NullReferenceException: 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:\Projects\CIS\xmlOut.xml")
        oRefNode = mdocNewXmlDoc2.SelectSingleNode("/GovTalkMessage/Body/IRenvelope/IRheader")
        oNode = mdocNewXmlDoc2.SelectSingleNode("/GovTalkMessage/Body/IRenvelope/IRheader/IRmark")
      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.ToString())
     End Try
0
Comment
Question by:1114godders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 18816184
The only thing that I do differently is

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("/Blah.Blah/BlehElement")

Darren
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18816189
Never mind the . in the path.  Once your path is correct.
0
 

Author Comment

by:1114godders
ID: 18816628
Darren,

many thanks for that, however i am still getting an "System.NullReferenceException: 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("/GovTalkMessage/Body/IRenvelope/IRheader/IRmark")

mdocNewXmlDoc2 = New XmlDocument
mdocNewXmlDoc2.Load("C:\Projects\CIS\xmlOut.xml")
oRefNode = mdocNewXmlDoc2.DocumentElement
oNode = oRefNode.SelectSingleNode("/GovTalkMessage/Body/IRenvelope/IRheader/IRmark")
        Try
            oRefNode.RemoveChild(oNode)
        Catch esc As SystemException
            MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
            Console.WriteLine(esc.ToString())
        End Try
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 18

Expert Comment

by:DarrenD
ID: 18816707
Can you put up you XML file??
0
 

Author Comment

by:1114godders
ID: 18816732
Sure,
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
  <EnvelopeVersion>2.0</EnvelopeVersion>
  <Header>
    <MessageDetails>
      <Class>IR-CIS-VERIFY</Class>
      <Qualifier>request</Qualifier>
      <Function>submit</Function>
      <CorrelationID/>
      <Transformation>XML</Transformation>
      <GatewayTest>1</GatewayTest>
      <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</PeriodEnd>
        <Principal>
          <Contact>
            <Name>
              <Ttl>Mr</Ttl>
              <Fore>Brian</Fore>
              <Sur>Cooper</Sur>
            </Name>
            <Email Preferred="yes">brian.cooper@wealden.net</Email>
            <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</DefaultCurrency>
        <IRmark Type="generic">                            </IRmark>
        <Sender>Employer</Sender>
      </IRheader>
      <CISrequest>
        <Contractor>
          <UTR>9325648154</UTR>
          <AOref>123PP87654321</AOref>
        </Contractor>
        <Subcontractor>
          <Action>verify</Action>
          <Type>company</Type>
          <TradingName>Plastered</TradingName>
          <WorksRef>ZA001</WorksRef>
          <CRN>NE987654</CRN>
        </Subcontractor>
        <Declaration>yes</Declaration>
      </CISrequest>
    </IRenvelope>
  </Body>
</GovTalkMessage>
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18817675
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:\Projects\CIS\xmlOut.xml")

            oRootNode = mdocNewXmlDoc2.DocumentElement()

            oNode = oRootNode.SelectSingleNode("/GovTalkMessage/Body/IRenvelope/IRheader/IRmark")


            If Not oNode Is Nothing Then
                oRootNode.RemoveChild(oNode)
            End If
        Catch esc As SystemException
            MsgBox("A system error ocurred" & vbNewLine & esc.ToString(), MsgBoxStyle.Critical)
            Console.WriteLine("************************************************")
            Console.WriteLine(esc.ToString())
        End Try
0
 

Author Comment

by:1114godders
ID: 18822307
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
0
 
LVL 18

Accepted Solution

by:
DarrenD earned 375 total points
ID: 18822510
Sorry,

What I ment was if you remove the

xmlns="http://www.govtalk.gov.uk/taxation/CISrequest"
and
xmlns="http://www.govtalk.gov.uk/CM/envelope"

from the XML file it works fine so there must be some workaround for this.

I don't know what it is though.

If you take out these lines from the XML your the code will work fine

Cheers,

Darren
0
 

Author Comment

by:1114godders
ID: 18822794
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.
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18823126
I know it's really wierd

Have a look here

http://pluralsight.com/blogs/keith/archive/2005/10/19/15714.aspx
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18823148
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
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18823179
Here's another article

http://www.devx.com/DevX/Tip/21337
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question