Solved

Setting InnerText for node.

Posted on 2004-10-26
1,034 Views
Last Modified: 2008-01-16
Im trying to pass an XMLNode into a function, but when I try to replace the contents of the node InnerText, I get the following exception:

"Object reference not set to an instance of an object"

Code fragment:

        protected XmlNode deLocaliseDeAmpersand(XmlNode xn)
        {            
                                               
            xn.InnerText = xn.InnerText.Replace("&", "$ampersand%");

xn.InnerText is valid as I can read the value inside but just can't set it and have tried using InnerXml instead but the same error.
0
Question by:rubans
    9 Comments
     
    LVL 15

    Expert Comment

    by:SRigney
    Is it possible that by replacing the & with $ampersand% you are making the xml string invalid?

    Maybe there is an & in the string that should not be getting replaced that is accidentally.

    If you can assign the inner text to itself, then that would indicate a data problem.
    xn.InnerText = xn.InnerText
    0
     

    Author Comment

    by:rubans
    Thanks for your reply...however, I tried just replacing the xn.InnerText with some valid XML e.g. <test/> and it still doesn't work...
    0
     
    LVL 15

    Expert Comment

    by:Thogek
    Silly question:
    Is there a reason to replace the "&" character with "$ampersand%"?  Wouldn't the standard XML-encoding of "&" to "&amp;" do?
    0
     
    LVL 15

    Accepted Solution

    by:
    How are you calling this deLocaliseDeAmpersand function?  Have you verified that the argument you pass in as xn does contain an actual XmlNode (and isn't null)?
    0
     
    LVL 15

    Expert Comment

    by:Thogek
    IOW, if you do:

        protected XmlNode deLocaliseDeAmpersand(XmlNode xn)
        {
            if(xn != null)
            {
                xn.InnerText = xn.InnerText.Replace("&", "$ampersand%");
            }

    do you still get the same error?
    0
     
    LVL 15

    Expert Comment

    by:SRigney
    I just played with it, and I have no problem replacing either innertext or innerxml with anything.

    I wouldn't want to replace inner xml though, because it returns &lt; for the < for all inner tags, so using it would probably cause problems.
    0
     

    Author Comment

    by:rubans
    Thogek, I followed your advice to ensure xn isn't null - it's not - and I get the same error.  I take your point about the XML encoding though, and am now using "&amp;" as the token.  

    I will probably end up looping through node-by-node instead.
    0
     
    LVL 8

    Expert Comment

    by:Razzie_
    rubans,

    are you sure THAT line is giving you that error? Because I don't think it should - that is perfectly fine code, even when the node text is empty!
    0
     

    Author Comment

    by:rubans
    Yep, I'm certain that line is failing.

    Thanks for your help.  The following is close to what I've gone with, which seems to work fine:

            protected XmlNode deLocaliseDeAmpersand(XmlNode xn)
            {            
                                                   
                XmlNodeReader reader =  new XmlNodeReader(xn);
                XmlDocument myXml = new XmlDocument();
               
                myXml.Load(reader);

                XmlNodeList myList = myXml.SelectNodes("//NewDataSet/Table/*");
                foreach (XmlElement a in myList)
                {
                      
                    a.InnerText = a.InnerText.Replace("&", "$ampersand%");
                }
               
                XmlNode xReturn = myXml.DocumentElement;

            return xReturn;
            }
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now