1

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>

1 Answer 1

2

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Imported namespaces work in XML Axis properties, but not when using LINQ to XML directly by calling methods.
Unfortunately id doesn't work. If you please look at xml I've added
Ok, your XML does not start with HANDERDETAILS in the root node, thus Result is returning empty. Change that line to 'var Result = ScriptDOC.Element("AAAAA").Element("HANDERDETAILS");' Also, the calls to Descendants could be changed to Element and eliminate the .First because Element returns the first one found rather than an IEnumerable of them. I've edited the code sample incorporating these changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.