Question

Could you help

Dim ScriptDOC As XDocument
    ScriptDOC = XDocument.Parse(e.Result)

Dim Result = From ele In XElement.Parse(ScriptDOC.ToString).Elements("HANDERDETAILS")
If IsNothing(Result.Descendants("RESULT")) = False Then status = Result.Descendants("RESULT").Value

If LCase(status) = "ok" Then
    If IsNothing(Result.Descendants("BRANCHID")) = False Then BranchID = Result.Descendants("BRANCHID").Value

End If

From converter I got this code:

XDocument ScriptDOC = default(XDocument);
string status = "";

ScriptDOC = XDocument.Parse(e.Result);

dynamic Result = from ele in XElement.Parse(ScriptDOC.ToString).Elements("HANDERDETAILS");
if ((Result.Descendants("RESULT") == null) == false)
    status = Result.Descendants("RESULT").Value;

if (Strings.LCase(status) == "ok") {
    if ((Result.Descendants("BRANCHID") == null) == false)
        BranchID = Result.Descendants("BRANCHID").Value;
}

which is not good.

This is my xml:

<AAAAA>
  <HANDERDETAILS>
    <RESULT>OK</RESULT>
    <BRANCHID>4</BRANCHID>
    <HANDLERID>1</HANDLERID>
    <HANDLERNAME>some Admin</HANDLERNAME>
    <BRANCHNAME>Asome New</BRANCHNAME>
  </HANDERDETAILS>
</AAAAA>
Was it helpful?

Solution

What converter did you use? It looks like it could be improved significantly. You do want to watch out for cases where you are recieving a collection but were expecting a single object which your original code does not appear to be handling.

XDocument ScriptDOC = XDocument.Parse(e.Result); 

var Result = ScriptDOC.Element("AAAAA").Element("HANDERDETAILS");
if (Result.Element("RESULT") != null)
{
    Status = Result.Element("RESULT").Value;
    if (Status.ToLower() == "ok")
        if (Result.Element("BRANCHID") != null)
            BranchID = Result.Element("BRANCHID").Value;
}

You also want to watch out for namespaces. In VB, you can declare them as an Import in your class, but in C#, you have to specify them explicitly in your code.

XNamespace ns = "http://www.someuri";
var Result = ScriptDOC.Elements(ns + "HANDERDETAILS");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top