471,089 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

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

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
3 14547
> 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
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
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.

Similar topics

22 posts views Thread by Christoph Boget | last post: by
6 posts views Thread by juli | last post: by
9 posts views Thread by Andy Sutorius | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.