简体   繁体   English

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

[英]How can I retrieve multiple elements from an XML file

I have the following xml that's returned when I do an export of TableStorage data using the Clumsy Leaf Table explorer: 我使用Clumsy Leaf Table资源管理器导出TableStorage数据时返回以下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>

I am using the following code: 我使用以下代码:

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(@"c:\data\contents.xml");
var titles =
    from entry in feed.Descendants(a + "entry")
    let partitionKey = entry.Element(d + "PartitionKey")
    let content = entry.Element(a + "content")
    let properties = content.Element(m + "properties")
    let title = properties.Element(d + "Title")

    select title;

foreach (var title in titles)
{
    Console.WriteLine(title.Value);
}
Console.ReadLine();

To get data from an XML file. 从XML文件获取数据。 The code works perfectly and gives me the value of all the elements: 代码完美运行并为我提供了所有元素的价值:

<d:Title>xxx</d:Title>

I would now like to get the value of the partitionKey also. 我现在也想获得partitionKey的值。 To do this I modified the line 为此,我修改了这条线

select title:

to

select partitionKey, title;

However this gives me an error: 但是这给了我一个错误:

"A local variable named 'title' cannot be declared in this scope because it would give a different meaning to 'title' which is already used in a child scope to denote something else. “名为'title'的局部变量不能在此范围内声明,因为它会给'title'赋予不同的含义,'title'已经在子范围中用来表示其他内容。

Can someone help and suggest how I could also get the values for partitionKey as well as title and display on the console. 有人可以帮助并建议我如何获得partitionKey的值以及控制台上的标题和显示。

You could use an anonymous type: 您可以使用匿名类型:

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);
}

or if you intend to pass the result of this LINQ query outside of the current method start by defining a model: 或者如果您打算通过定义模型来传递当前方法之外的LINQ查询的结果:

public class Result
{
    public string Result { get; set; }
    public string PartitionKey { get; set; }
}

and then project the LINQ query to an IEnumerable<Result> : 然后将LINQ查询投影到IEnumerable<Result>

select new Result
{
    Title = title,
    PartitionKey = partitionKey
};

UPDATE: 更新:

Now that you have updated your answer and shown the XML you are dealing with it seems that PartitionKey is a subnode of <m:properties> , so why are you using let partitionKey = entry.Element(d + "PartitionKey") ? 现在您已经更新了答案并显示了您正在处理的XML,似乎PartitionKey是<m:properties>的子let partitionKey = entry.Element(d + "PartitionKey") ,那么为什么使用let partitionKey = entry.Element(d + "PartitionKey") In example entry represents the <entry> node. 在示例中, entry表示<entry>节点。 I have updated my previous code sample to match your XML. 我更新了我之前的代码示例以匹配您的XML。 You should be more careful/consistent in your LINQ queries: 您应该在LINQ查询中更加小心/一致:

let partitionKey = properties.Element(d + "PartitionKey")
select partitionKey, title;

does not do what you think it does. 不会做你认为它做的事情。 It looks like you want to return instances of an anonymous type with two properties: partitionKey and title . 看起来您想要返回具有两个属性的匿名类型的实例: partitionKeytitle To do this, you must declare the anonymous type explicitly, ie 为此,您必须明确声明匿名类型,即

select new { partitionKey, title };

As to what your line of code actually does, based on the compiler error text, I think that it is simply illegal syntax. 至于你的代码行实际上做了什么,基于编译器错误文本,我认为它只是非法的语法。

NB: The VB.NET compiler is a bit more forgiving. 注意:VB.NET编译器有点宽容。 It does treat Select partitionKey, title as syntactic sugar for Select New With { partitionKey, title } , which is what you would expect. 它确实将Select partitionKey, title作为Select New With { partitionKey, title }语法糖处理,这是你所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM