I am trying to write a Linq query which will parse my XML tree(which is actually a SQL deconstructed into an XML tree).
My XML looks like this
<SqlRoot>
<SqlStatement>
<Clause>
<OtherKeyword>select</OtherKeyword>
<WhiteSpace></WhiteSpace>
<Other>t1</Other>
<Period>.</Period>
<Other>empid</Other>
<WhiteSpace></WhiteSpace>
</Clause>
<Clause>
<OtherKeyword>from</OtherKeyword>
<SelectionTarget>
<WhiteSpace></WhiteSpace>
<Other>bd_orm</Other>
<Period>.</Period>
<Other>dbo</Other>
<Period>.</Period>
<Other>bal_impacts_t</Other>
<WhiteSpace></WhiteSpace>
<Other>t1</Other>
</SelectionTarget>
</Clause>
</SqlStatement>
</SqlRoot>
I am trying to pick out the table name ( SelectionTarget
node). The WhiteSpace
node is a blank/white space in between the values.
So the output which I expect is something like this bd_orm.dbo.bal_impacts_t t1
but I am unable to figure out how to do this by including a whitespace
in between.
I tried this
var xxx = (from res in xDoc.Descendants("Clause").Descendants("SelectionTarget") select res);
Console.WriteLine(xxx.DescendantNodesAndSelf().OfType<XElement>().First().Value);
but it obviously fail cause I do not know how to take into account the whitespace
node and convert that into an actual whitespace
. Any suggestions?
var nodes = (from res in xDoc.Descendants("Clause")
.Descendants("SelectionTarget")
.Descendants()
select res);
string name = String.Join("", nodes.Select(n=>n.Name == "WhiteSpace"?" ":n.Value));
name: bd_orm.dbo.bal_impacts_t t1
nodes:
<WhiteSpace></WhiteSpace>
<Other>bd_orm</Other>
<Period>.</Period>
<Other>dbo</Other>
<Period>.</Period>
<Other>bal_impacts_t</Other>
<WhiteSpace></WhiteSpace>
<Other>t1</Other>
Simply select a space for the WhiteSpace
nodes and the string value for all other nodes, then concatenate the results:
var parts = doc.Descendants("SelectionTarget")
.Elements()
.Select(e => e.Name == "WhiteSpace" ? " " : (string)e);
var text = string.Concat(parts);
You could add the whitespace before constructing the query:
foreach (var ws in xDoc.Descendants("WhiteSpace"))
{
ws.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.