LINQ to XML - Elements() 有效,但 Elements(XName) 无效
下面给出的是我的 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();
蒂亚。
拉贾
Given below is my 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>
I want to get all the Textbox names and values. This is what I tried and it does not work:
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();
but when I change the query to something like this
var elements = from c in data.Elements().Elements().ElementAt(0).Elements().ElementAt(0).Elements()
select c;
it works.
Any help in this regard is much appreciated.
Edit: With the help of answers I was able to get the desired results. Thank you so much :)
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.
Raja
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要考虑命名空间:
然后使用 df + "foo" 在根元素上定义的命名空间中选择具有本地名称
foo
的元素。正如已经提到的,您可能想要选择后代,而不是子元素:
You need to take the namespace into account:
Then use
df + "foo"
to select elements with local namefoo
in the namespace defined on the root element.And as already mentioned you probably want to select descendants, not child elements:
在这种情况下,您正在寻找
Descendants()
而不是Elements()
。Elements()
仅选择直接子级。文档
注意:根据您的示例代码,使用
Descendants()
仍会引发异常,因为并非所有ReportItems
元素都有Name
> 属性。您需要执行类似Console.WriteLine("Element : " + (element.Attributes("Name").Any() ? element.Attribute("Name").Value : "(no name)") 的操作);
You are looking for
Descendants()
notElements()
in this case.Elements()
only selects immediate children.Documentation
Note: Based on your sample code, using
Descendants()
will still throw an exception because not all of theReportItems
elements have aName
attribute. You need to do something likeConsole.WriteLine("Element : " + (element.Attributes("Name").Any() ? element.Attribute("Name").Value : "(no name)") );
只需将 XElements() 更改为 Descendants(),它可能会起作用
just change XElements() to Descendants(),it will work probably