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.
rubansAsked:
Who is Participating?
 
ThogekCommented:
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
 
SRigneyCommented:
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
 
rubansAuthor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ThogekCommented:
Silly question:
Is there a reason to replace the "&" character with "$ampersand%"?  Wouldn't the standard XML-encoding of "&" to "&amp;" do?
0
 
ThogekCommented:
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
 
SRigneyCommented:
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
 
rubansAuthor Commented:
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
 
Razzie_Commented:
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
 
rubansAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.