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(XmlN ode 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.
"Object reference not set to an instance of an object"
Code fragment:
protected XmlNode deLocaliseDeAmpersand(XmlN
{
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.
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 "&" do?
Is there a reason to replace the "&" character with "$ampersand%"? Wouldn't the standard XML-encoding of "&" to "&" do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
IOW, if you do:
protected XmlNode deLocaliseDeAmpersand(XmlN ode xn)
{
if(xn != null)
{
xn.InnerText = xn.InnerText.Replace("&", "$ampersand%");
}
do you still get the same error?
protected XmlNode deLocaliseDeAmpersand(XmlN
{
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 < for the < for all inner tags, so using it would probably cause problems.
I wouldn't want to replace inner xml though, because it returns < for the < for all inner tags, so using it would probably cause problems.
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 "&" as the token.
I will probably end up looping through node-by-node instead.
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!
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!
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(XmlN ode xn)
{
XmlNodeReader reader = new XmlNodeReader(xn);
XmlDocument myXml = new XmlDocument();
myXml.Load(reader);
XmlNodeList myList = myXml.SelectNodes("//NewDa taSet/Tabl e/*");
foreach (XmlElement a in myList)
{
a.InnerText = a.InnerText.Replace("&", "$ampersand%");
}
XmlNode xReturn = myXml.DocumentElement;
return xReturn;
}
Thanks for your help. The following is close to what I've gone with, which seems to work fine:
protected XmlNode deLocaliseDeAmpersand(XmlN
{
XmlNodeReader reader = new XmlNodeReader(xn);
XmlDocument myXml = new XmlDocument();
myXml.Load(reader);
XmlNodeList myList = myXml.SelectNodes("//NewDa
foreach (XmlElement a in myList)
{
a.InnerText = a.InnerText.Replace("&", "$ampersand%");
}
XmlNode xReturn = myXml.DocumentElement;
return xReturn;
}
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