C# 如何从XML文件中检索多个元素

C# 如何从XML文件中检索多个元素,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,当我使用笨拙的叶表资源管理器导出表存储数据时,返回了以下xml: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <feed xml:base="http://x.table.core.windows.net/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.

当我使用笨拙的叶表资源管理器导出表存储数据时,返回了以下xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://x.table.core.windows.net/" 
      xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
      xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
      xmlns="http://www.w3.org/2005/Atom">
    <title type="text">TestContents</title>
    <updated />
    <link rel="self" title="TestContents" href="TestContents" />

    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit"  />
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>010100A</d:PartitionKey>
                <d:Title>ssq</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit"  />
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>010100B</d:PartitionKey>
                <d:Title>yy</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit" />
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>010100C</d:PartitionKey>
                <d:Title>xx</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
</feed>
从XML文件获取数据。代码运行得非常完美,并为我提供了所有 要素:

xxx

我现在还想得到partitionKey的值。为此,我修改了线路

select title:

但是,这给了我一个错误:

“名为'title'的局部变量不能在此范围中声明,因为它将赋予已在子范围中用于表示其他内容的'title'不同的含义

有人可以帮助我并建议我如何在控制台上获取partitionKey以及title和display的值

select partitionKey, title;
与您认为的不同。您似乎希望返回具有两个属性的匿名类型的实例:
partitionKey
title
。为此,必须显式声明匿名类型,即

select new { partitionKey, title };
至于你的代码行实际上做了什么,基于编译器的错误文本,我认为这只是非法的语法


注意:VB.NET编译器有点宽容。它确实将
Select partitionKey,title
视为
Select New With{partitionKey,title}
的语法糖,这是您所期望的。

您可以使用匿名类型:

XNamespace a = "http://www.w3.org/2005/Atom";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XElement feed = XElement.Load("test.xml");
var result =
    from entry in feed.Descendants(a + "entry")
    let content = entry.Element(a + "content")
    let properties = content.Element(m + "properties")
    let title = properties.Element(d + "Title")
    let partitionKey = properties.Element(d + "PartitionKey")
    select new
    {
        Title = title.Value,
        PartitionKey = partitionKey.Value,
    };

foreach (var item in result)
{
    Console.WriteLine("{0}, {1}", item.Title, item.PartitionKey);
}
或者,如果要将此LINQ查询的结果传递到当前方法之外,请从定义模型开始:

public class Result
{
    public string Result { get; set; }
    public string PartitionKey { get; set; }
}
然后将LINQ查询投影到一个
IEnumerable


更新:

既然您已经更新了答案并显示了正在处理的XML,那么PartitionKey似乎是
的子节点,那么为什么要使用
让PartitionKey=entry.Element(d+“PartitionKey”)
?在示例
中,entry
表示
节点。我已更新了以前的代码示例以匹配您的XML。在LINQ查询中,您应该更加小心/一致:

let partitionKey = properties.Element(d + "PartitionKey")

我尝试了这个方法,但出现了一些不寻常的情况。d:Title在命名空间中找到,但PartitionKey没有。因此,它会为所有分区键返回null。好吧,如果您显示XML文件并解释了在您的特定情况下PartitionKey是什么,我们可能可以提供帮助。现在您似乎正在谈论一些PartitionKey,而您似乎e是唯一知道这是什么的人。我希望你意识到我们无法读懂你的心思,你试图解析的XML文件以及你的代码不起作用的原因。我将用XML更新这个问题。很抱歉我延迟答复(这里的internet连接糟糕),也很抱歉没有包括在内。以下是我在结果中得到的结果:{Title={您正在从entry节点选择PartitionKey,这显然毫无意义,因为
的子节点,而在您的代码中,您使用的是
让PartitionKey=entry.Element(d+“PartitionKey”)
,但是条目当然是
节点。您应该在LINQ查询中更加一致。我已经更新了我的初始代码示例以匹配您的特定XML。希望这会有所帮助。非常感谢Darin。我没有看到它来自条目。我的错误副本/粘贴。我将标记为已接受。
public class Result
{
    public string Result { get; set; }
    public string PartitionKey { get; set; }
}
select new Result
{
    Title = title,
    PartitionKey = partitionKey
};
let partitionKey = properties.Element(d + "PartitionKey")