?
Solved

Setting InnerText for node.

Posted on 2004-10-26
9
Medium Priority
?
1,106 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
Comment
Question by:rubans
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 12413141
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
ID: 12413224
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
ID: 12413244
Silly question:
Is there a reason to replace the "&" character with "$ampersand%"?  Wouldn't the standard XML-encoding of "&" to "&amp;" do?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 15

Accepted Solution

by:
Thogek earned 750 total points
ID: 12413254
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
ID: 12413280
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
ID: 12413352
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
ID: 12419751
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_
ID: 12419881
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
ID: 12420252
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

601 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