简体   繁体   中英

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!

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;

Lexical converters have trouble distinguishing VB's function calls from indexers, which use the same syntax in VB but different syntax in C#:

XmlElement headerElement = document.DocumentElement["ReplayHeader"];
string errorMessage = headerElement.Attributes["errorMessage"].Value;
errorCode = headerElement.Attributes["returnCode"].Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM