Read XML using LINQ using formatting

114 views Asked by At

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?

3

There are 3 answers

0
Charles Mager On BEST ANSWER

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);
0
ASh On
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

demo

nodes:

<WhiteSpace></WhiteSpace>
<Other>bd_orm</Other>
<Period>.</Period>
<Other>dbo</Other>
<Period>.</Period>
<Other>bal_impacts_t</Other>
<WhiteSpace></WhiteSpace>
<Other>t1</Other>
0
Andrei V On

You could add the whitespace before constructing the query:

 foreach (var ws in xDoc.Descendants("WhiteSpace"))
 {
      ws.Value = " ";
 }