Link to home
Start Free TrialLog in
Avatar of rubans
rubans

asked on

Setting InnerText for node.

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.
Avatar of SRigney
SRigney
Flag of United States of America image

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
Avatar of rubans
rubans

ASKER

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...
Silly question:
Is there a reason to replace the "&" character with "$ampersand%"?  Wouldn't the standard XML-encoding of "&" to "&amp;" do?
ASKER CERTIFIED SOLUTION
Avatar of Thogek
Thogek
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.
Avatar of rubans

ASKER

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.
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!
Avatar of rubans

ASKER

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;
        }