Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to XML - Elements() works but Elements(XName) does not work
    primarykey
    data
    text
    <p>Given below is my xml: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition"&gt; &lt;Body&gt; &lt;ReportItems&gt; &lt;Textbox Name="txtCurrentDate"&gt; &lt;CanGrow&gt;true&lt;/CanGrow&gt; &lt;KeepTogether&gt;true&lt;/KeepTogether&gt; &lt;Paragraphs&gt; &lt;Paragraph&gt; &lt;TextRuns&gt; &lt;TextRun&gt; &lt;Value&gt;=Today()&lt;/Value&gt; &lt;Style&gt; &lt;FontWeight&gt;Medium&lt;/FontWeight&gt; &lt;Format&gt;d&lt;/Format&gt; &lt;/Style&gt; &lt;/TextRun&gt; &lt;/TextRuns&gt; &lt;Style&gt; &lt;TextAlign&gt;Left&lt;/TextAlign&gt; &lt;/Style&gt; &lt;/Paragraph&gt; &lt;/Paragraphs&gt; &lt;Left&gt;0.36958in&lt;/Left&gt; &lt;Height&gt;0.22917in&lt;/Height&gt; &lt;Width&gt;1in&lt;/Width&gt; &lt;Style&gt; &lt;Border&gt; &lt;Style&gt;None&lt;/Style&gt; &lt;/Border&gt; &lt;PaddingLeft&gt;2pt&lt;/PaddingLeft&gt; &lt;PaddingRight&gt;2pt&lt;/PaddingRight&gt; &lt;PaddingTop&gt;2pt&lt;/PaddingTop&gt; &lt;PaddingBottom&gt;2pt&lt;/PaddingBottom&gt; &lt;/Style&gt; &lt;/Textbox&gt; &lt;Textbox Name="txtName"&gt; &lt;CanGrow&gt;true&lt;/CanGrow&gt; &lt;KeepTogether&gt;true&lt;/KeepTogether&gt; &lt;Paragraphs&gt; &lt;Paragraph&gt; &lt;TextRuns&gt; &lt;TextRun&gt; &lt;Value&gt;Mark Wilkinson&lt;/Value&gt; &lt;Style /&gt; &lt;/TextRun&gt; &lt;/TextRuns&gt; &lt;Style /&gt; &lt;/Paragraph&gt; &lt;/Paragraphs&gt; &lt;Top&gt;0.22917in&lt;/Top&gt; &lt;Left&gt;0.36958in&lt;/Left&gt; &lt;Height&gt;0.20833in&lt;/Height&gt; &lt;Width&gt;3.22917in&lt;/Width&gt; &lt;ZIndex&gt;1&lt;/ZIndex&gt; &lt;Style&gt; &lt;Border&gt; &lt;Style&gt;None&lt;/Style&gt; &lt;/Border&gt; &lt;PaddingLeft&gt;2pt&lt;/PaddingLeft&gt; &lt;PaddingRight&gt;2pt&lt;/PaddingRight&gt; &lt;PaddingTop&gt;2pt&lt;/PaddingTop&gt; &lt;PaddingBottom&gt;2pt&lt;/PaddingBottom&gt; &lt;/Style&gt; &lt;/Textbox&gt; &lt;/ReportItems&gt; &lt;Height&gt;6.01667in&lt;/Height&gt; &lt;Style /&gt; &lt;/Body&gt; &lt;Width&gt;7.92333in&lt;/Width&gt; &lt;/Report&gt; </code></pre> <p>I want to get all the <strong>Textbox</strong> names and values. This is what I tried and it does not work:</p> <pre><code>XDocument data = XDocument.Load("..\\..\\testxml.rdl"); var elements = from c in data.Elements("ReportItems") select c; foreach (var element in elements) { Console.WriteLine("Element : " + element.Attribute("Name").Value); } Console.ReadKey(); </code></pre> <p>but when I change the query to something like this</p> <pre><code>var elements = from c in data.Elements().Elements().ElementAt(0).Elements().ElementAt(0).Elements() select c; </code></pre> <p>it works.</p> <p>Any help in this regard is much appreciated.</p> <p>Edit: With the help of answers I was able to get the desired results. Thank you so much :)</p> <pre><code>XDocument data = XDocument.Load("..\\..\\testxml.rdl"); XNamespace ns = data.Root.Name.Namespace; var elements = from c in data.Descendants(ns + "Textbox") select c; foreach (var element in elements) { Console.WriteLine("Element : " + element.Attribute("Name").Value); } Console.ReadKey(); </code></pre> <p>TIA.</p> <p>Raja</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload