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

x
?
Solved

Setting InnerText for node.

Posted on 2004-10-26
9
Medium Priority
?
1,095 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

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