开发者

Converting linq-to-xml query from vb to c#

开发者 https://www.devze.com 2023-04-01 14:12 出处:网络
Could you help Dim ScriptDOC As XDocument ScriptDOC = XDocument.Parse(e.Result) Dim Result = From ele In XElement.Parse(ScriptDOC.ToString).Elements(\"HANDERDETAILS\")

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>开发者_如何学Go;
    <HANDLERNAME>some Admin</HANDLERNAME>
    <BRANCHNAME>Asome New</BRANCHNAME>
  </HANDERDETAILS>
</AAAAA>


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消