VB to C#

  • Thread starter J André Labuschagné
  • Start date
J

J André Labuschagné

Hi All

I have a line of code that works in VB.NET but I get the following error
when converted to C#. The code in VB is:

Dim elementToEncryptDOM As XmlElement =
xmlDocument.GetElementByTagName(elementToEncrypt)(0)

elementToEncrypt is a string passed to the function

My incorrect code in C# is:

XmlDocument xmlDocument = new XmlDocument();
XmlElement elementToEncryptDOM =
xmlDocument.GetElementsByTagName(elementToEncrypt)[0];

The error I get is:

Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlElement'. An explicit conversion exists (are you missing a
cast?)

System.Xml is included.

I think I am missing a new somewhere.

I am really hoping someone can throw light on this.

Cheers
Andre
 
M

Martin Honnen

J said:
I have a line of code that works in VB.NET but I get the following error
when converted to C#. The code in VB is:

Dim elementToEncryptDOM As XmlElement =
xmlDocument.GetElementByTagName(elementToEncrypt)(0)

elementToEncrypt is a string passed to the function

My incorrect code in C# is:

XmlDocument xmlDocument = new XmlDocument();
XmlElement elementToEncryptDOM =
xmlDocument.GetElementsByTagName(elementToEncrypt)[0];

The error I get is:

Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlElement'. An explicit conversion exists (are you missing
a cast?)

GetElementsByTagName returns an XmlNodeList and each item you find in
the list is an XmlNode so you need to cast to XmlElement e.g.

XmlElement elementToEncryptDOM =
(XmlElement)xmlDocument.GetElementsByTagName(elementToEncrypt)[0];

or

XmlElement elementToEncryptDOM =
xmlDocument.GetElementsByTagName(elementToEncrypt)[0] as XmlElement;
 
J

J André Labuschagné

Hi Martin

You, my dear sir, are a genius. You have probably guessed that my
experience is not with managed code at all.

Standby - I may have a few more lines.

Thanks so much.

Cheers
Andre

Martin Honnen said:
J said:
I have a line of code that works in VB.NET but I get the following error
when converted to C#. The code in VB is:

Dim elementToEncryptDOM As XmlElement =
xmlDocument.GetElementByTagName(elementToEncrypt)(0)

elementToEncrypt is a string passed to the function

My incorrect code in C# is:

XmlDocument xmlDocument = new XmlDocument();
XmlElement elementToEncryptDOM =
xmlDocument.GetElementsByTagName(elementToEncrypt)[0];

The error I get is:

Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Xml.XmlElement'. An explicit conversion exists (are you missing a
cast?)

GetElementsByTagName returns an XmlNodeList and each item you find in the
list is an XmlNode so you need to cast to XmlElement e.g.

XmlElement elementToEncryptDOM =
(XmlElement)xmlDocument.GetElementsByTagName(elementToEncrypt)[0];

or

XmlElement elementToEncryptDOM =
xmlDocument.GetElementsByTagName(elementToEncrypt)[0] as XmlElement;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top