By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlAttribute'

P: n/a
I have the foll. code to update the value of an attribute:

xmlDocument.Load("abc.xml");
XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc);
xmlAttrib.Value = ddDes.SelectedItem.ToString();
xmlDocument.Save("abc.xml");

However, I get the foll. error:
Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlAttribute'

What code should be modified so that the error is resolved.

Thanks.

Nov 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> XmlAttribute xmlAttrib =
xmlDocument.SelectSingleNode(root/web/theme/@desc);

should be

XmlAttribute xmlAttrib = (XmlAttribute)
xmlDocument.SelectSingleNode(root/web/theme/@desc);
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Anita C" <an****@ptm-software.com> wrote in message
news:#N**************@tk2msftngp13.phx.gbl...
I have the foll. code to update the value of an attribute:

xmlDocument.Load("abc.xml");
XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value = ddDes.SelectedItem.ToString();
xmlDocument.Save("abc.xml");

However, I get the foll. error:
Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlAttribute'

What code should be modified so that the error is resolved.

Thanks.

Nov 11 '05 #2

P: n/a
Anita,

SelectSingleNode returns an XmlNode object. Since XmlNode is a superclass of
XmlAttribute the compiler does not do the type casting for you. You have to
explicitly downcast the returned object such as:

XmlAttribute xmlAttrib = (XmlAttribute)
xmlDocument.SelectSingleNode(root/web/theme/@desc);

or

XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc)
as XmlAttribute;
if you are new to object oriented programming and strongly typed languages I
strongly recommend you get a good book on the topic or at least start with
the .NET framwork SDK documentation which is installed on your local disk.
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Anita C" <an****@ptm-software.com> wrote in message
news:#N**************@tk2msftngp13.phx.gbl...
I have the foll. code to update the value of an attribute:

xmlDocument.Load("abc.xml");
XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value = ddDes.SelectedItem.ToString();
xmlDocument.Save("abc.xml");

However, I get the foll. error:
Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlAttribute'

What code should be modified so that the error is resolved.

Thanks.

Nov 11 '05 #3

P: n/a
Thanks for all the help.

"Anita C" <an****@ptm-software.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have the foll. code to update the value of an attribute:

xmlDocument.Load("abc.xml");
XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value = ddDes.SelectedItem.ToString();
xmlDocument.Save("abc.xml");

However, I get the foll. error:
Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlAttribute'

What code should be modified so that the error is resolved.

Thanks.

Nov 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.