intA intA - 1 year ago 72 Question

How do I convert the following from VB to C# .net?

I'm converting some code from VB to C#, everything is going pretty fine until the following lines related to XmlElements. This is the VB:

Dim headerElement As XmlElement = document.DocumentElement("ReplyHeader")
Dim errorMessage As String = headerElement.Attributes("errorMessage").Value
errorCode = headerElement.Attributes("returnCode").Value

And this is how I converted it into C#

XmlElement headerElement = document.DocumentElement("ReplayHeader");
string errorMessage = headerElement.Attributes("errorMessage").Value;
errorCode = headerElement.Attributes("returnCode").Value;

But I am getting errors like "Non-invocable member 'System.Xml.XmlDocument.DocumentElement' cannot be used like a method." and "System.Xml.XmlElement.Attributes is a 'property' but is used like a 'method'"

My issue really stems from not toatally understanding what the VB code is trying to do here. For instance, the first line, is it creating an XmlElement containing the text "ReplyHeader"? If so, how do I do this in C#?

Thanks for any help!

Answer Source

You want brackets, in c# the pair of () denotes a FUNCTION call. To access elements you need to use bracket [] Indexers

XmlElement headerElement = document.DocumentElement["ReplayHeader"];
string errorMessage = headerElement.Attributes["errorMessage"].Value;
errorCode = headerElement.Attributes["returnCode"].Value;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download