Connecting Tech Pros Worldwide Forums | Help | Site Map

What is the difference between XmlNode and XmlElement?

Pluto
Guest
 
Posts: n/a
#1: Nov 11 '05
What is the difference between XmlNode and XmlElement?

I'm trying to implement the code at
http://www.devx.com/dotnet/Article/11616/0/page/3 in C# and I'm getting an
error at this line:
this.appSettingsElement =
this.xml.SelectSingleNode("//configuration/appSettings");

Error: Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlElement'

Thanks.



Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 11 '05

re: What is the difference between XmlNode and XmlElement?


Pluto wrote:
[color=blue]
> What is the difference between XmlNode and XmlElement?[/color]
The same as between fruit and apple. XmlElement is particular type of
XmlNode.
[color=blue]
> this.appSettingsElement =
> this.xml.SelectSingleNode("//configuration/appSettings");
>
> Error: Cannot implicitly convert type 'System.Xml.XmlNode' to
> 'System.Xml.XmlElement'[/color]

So convert it explicitly:
this.appSettingsElement =
(XmlElement)this.xml.SelectSingleNode("//configuration/appSettings");

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Peter Rilling
Guest
 
Posts: n/a
#3: Nov 11 '05

re: What is the difference between XmlNode and XmlElement?


But as for the difference between the two, a node is more general then an
element. Anything in an XML document is a node (e.g. attributes, comments,
doctype, etc), but only tags are elements.

This error is not specific to XML conversions but .NET cannot implicitly
down-cast a variable. That is why you need to specify the cast yourself
since SelectSingleNode returns an XmlNode object.

"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:e7Q3XKSjDHA.2592@TK2MSFTNGP10.phx.gbl...[color=blue]
> Pluto wrote:
>[color=green]
> > What is the difference between XmlNode and XmlElement?[/color]
> The same as between fruit and apple. XmlElement is particular type of
> XmlNode.
>[color=green]
> > this.appSettingsElement =
> > this.xml.SelectSingleNode("//configuration/appSettings");
> >
> > Error: Cannot implicitly convert type 'System.Xml.XmlNode' to
> > 'System.Xml.XmlElement'[/color]
>
> So convert it explicitly:
> this.appSettingsElement =
> (XmlElement)this.xml.SelectSingleNode("//configuration/appSettings");
>
> --
> Oleg Tkachenko
> http://www.tkachenko.com/blog
> Multiconn Technologies, Israel
>[/color]


Closed Thread


Similar .NET Framework bytes