简体   繁体   中英

Using XPathEvaluate to get text node value in Linq-to-Xml query

I recently came across a situation where I had to use XPath as part of a Linq to XML query and found that it acts a bit strange. I am sure there are other ways of solving the problem using the native XElement API methods, but in this situation XPath was a much better fit for locating the specific XML text values that I needed as the document structure was quite complex. I eventually found a way to make it work (see below), but it was not very pretty, which usually means I missed something.

My question is how can you use XPathEvalute or some other XPath method to return a string value that can be used in a where statement or as part of a new object in the select method. I found a solution that uses a lot of casting and the Null coalesing operator in LINQ , but there has to be a cleaner way. The root of the problem is that the XPathEvaluate method would usually return an XText element (casted to a generic object), but Linq converts it to an IEnumerable so that it can delay the execution. This causes problem when you try to access the value of that object, as it does not get materialized until the query is executed.

Here is the example: Using the following XML document find all parents that have a Boy and Girl with the same name. This MUST be done using an XPath query, as in my situation the document structure was a lot complex.

//Use C# Program Language type in LinqPad
void Main()
{
    var xml = XElement.Parse(@"<Root>
        <Parent>
            <Boy>Anne</Boy>
        </Parent>
        <Parent>
            <Boy>Alex</Boy>
            <Girl>Alex</Girl>
        </Parent>
        <Parent>
            <Boy>Jay</Boy>
        </Parent>
    </Root>");

    var q1 = from e in xml.XPathSelectElements("/Parent")
             select e;

    q1.Dump();

    var q2 = from e in q1
             let boy = ((IEnumerable<Object>)e.XPathEvaluate("Boy/text()")).Cast<System.Xml.Linq.XText>().FirstOrDefault() ?? new System.Xml.Linq.XText("")
             let girl = ((IEnumerable<Object>)e.XPathEvaluate("Girl/text()")).Cast<System.Xml.Linq.XText>().FirstOrDefault() ?? new System.Xml.Linq.XText("")
             select new {t=boy.GetType(), b=boy, g=girl, same=(boy.Value==girl.Value)};

    q2.Dump();
}

Results are:

在此处输入图像描述

You can see that the second element is identified as having a boy and girl with the same name. Is there any cleaner way of doing this while still using XPath or am I stuck using the code from above.

Is it acceptable to use XPathSelectElement instead of XPathEvaluate ?

var q2 = from e in q1
         let boy = (string)e.XPathSelectElement("Boy")
         let girl = (string)e.XPathSelectElement("Girl")
         where boy.StartsWith("A") //Optional use in where statement
         select new { t = boy.GetType(), b = boy, g = girl, same = (boy == girl) };

How about this, not sure if you need 't'.

var q2 = q1.Select (q => 
    new 
    { 
        Boy  = q.XPathSelectElement("Boy"),
        Girl = q.XPathSelectElement("Girl"),
    }
);

var q3 = q2.Select (q => 
    new 
    {
        Boy = q.Boy == null ? string.Empty : q.Boy.Value, 
        Girl = q.Girl == null ? string.Empty : q.Girl.Value
    } 
);

var q4 = q3.Select (q => new { q.Boy, q.Girl, same=q.Boy==q.Girl });

q4.Dump();

Which can be rewritten as

var q2 = q1.
    Select (q => new { Boy  = q.XPathSelectElement("Boy"),Girl = q.XPathSelectElement("Girl"),}).
    Select (q => new { Boy = q.Boy == null ? string.Empty : q.Boy.Value, Girl = q.Girl == null ? string.Empty : q.Girl.Value}).
    Select (q => new { q.Boy, q.Girl, same=q.Boy==q.Girl }
);

q2.Dump();

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