以下是我的xml:
<?xml version="1.0" encoding="utf-8"?> <Report xmlns:rd="http://schemas.microsoft.com/sqlServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition"> <Body> <ReportItems> <TextBox Name="txtCurrentDate"> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>=Today()</Value> <Style> <FontWeight>Medium</FontWeight> <Format>d</Format> </Style> </TextRun> </TextRuns> <Style> <TextAlign>Left</TextAlign> </Style> </Paragraph> </Paragraphs> <Left>0.36958in</Left> <Height>0.22917in</Height> <Width>1in</Width> <Style> <Border> <Style>None</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </TextBox> <TextBox Name="txtName"> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>Mark Wilkinson</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <Top>0.22917in</Top> <Left>0.36958in</Left> <Height>0.20833in</Height> <Width>3.22917in</Width> <ZIndex>1</ZIndex> <Style> <Border> <Style>None</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </TextBox> </ReportItems> <Height>6.01667in</Height> <Style /> </Body> <Width>7.92333in</Width> </Report>
我想获取所有的TextBox名称和值.这是我尝试的,它不工作:
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();
但是当我将查询更改为这样的时候
var elements = from c in data.Elements().Elements().ElementAt(0).Elements().ElementAt(0).Elements() select c;
有用.
在这方面的任何帮助都非常感谢.
编辑:在答案的帮助下,我得到了所需的结果.非常感谢 :)
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();
TIA.
拉贾