LINQ to XML - Elements()有效但Elements(XName)不起作用

时间:2011-06-02 12:20:09

标签: c# xml linq-to-xml

以下是我的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。

拉​​加

3 个答案:

答案 0 :(得分:30)

您需要考虑名称空间:

XNamespace df = data.Root.Name.Namespace;

然后使用df + "foo"在根元素上定义的命名空间中选择具有本地名称foo的元素。

如前所述,您可能想要选择后代,而不是子元素:

        var elements = from c in data.Descendants(df + "Textbox")
                       select c;

答案 1 :(得分:4)

在这种情况下,您正在寻找Descendants()而不是Elements()Elements()只选择直接的孩子。

<强>文档

注意:根据您的示例代码,使用Descendants()仍会引发异常,因为并非所有ReportItems元素都具有Name属性。您需要执行Console.WriteLine("Element : " + (element.Attributes("Name").Any() ? element.Attribute("Name").Value : "(no name)") );

之类的操作

答案 2 :(得分:0)

 if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filepath = openFileDialog1.FileName;
            try
            {

                XElement xe = XElement.Load(filepath);
              // 

                string sql = "insert OR IGNORE into price_list (name,his_code,specs,unit,spell,price) values(@name,@his_code,@specs,@unit,@spell,@price);";
                int count = 0;
                foreach (XElement el in xe.Descendants("row"))
                {
                    SQLiteParameter[] ps =
                    {
                        new SQLiteParameter("@his_code",el.Element("aka079").Value),
                        new SQLiteParameter("@name",el.Element("aka061").Value),
                        new SQLiteParameter("@specs",el.Element("aka073").Value),
                        new SQLiteParameter("@unit",el.Element("aka067").Value),
                        new SQLiteParameter("@spell",SpellCode.GetSpellCode(el.Element("aka061").Value)),
                        new SQLiteParameter("@price",el.Element("aka071").Value),
                    };

                    if (SqliteHelper.ExecuteNonQuery(sql, ps) > 0)
                    {
                        count++;
                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }

只需将XElements()更改为Descendants(),它可能会起作用