从2个不同的xml标签中读取相同的属性

时间:2016-07-18 10:15:26

标签: c# xml attributes

我有一个包含一些数据的xml,我想提取一些信息,但我无法弄清楚如何。我将在下面发布一个例子:

<ns:Taggg ns:SelectThisValues="3421" xmlns:ns="namespaceURL">3421<ns:AnotherTaggg ns:SomeValue="3421" />
</ns:Taggg>

    <ns:Taggg2 ns:SelectThisValues="3422" xmlns:ns="namespaceURL">3422<ns:AnotherTaggg ns:SomeValue="3421" />
</ns:Taggg2>

基于此,我想从&#39; SelectThisValues&#39;中选择值。属性,但来自两个标签,仅在一个操作中。

在此示例中,命名空间定义为ns。

我对xml没有太多经验,但我知道这是可能的,只是,我不知道如何做到这一点。有谁能够帮我?欢迎任何想法!

我不仅需要基本的想法,我可以处理复杂的想法

1 个答案:

答案 0 :(得分:1)

您可以使用LINQ to XML轻松完成此任务:

XNamespace ns = "namespaceURL";

var doc = XDocument.Parse(xmlString);

var values = doc.Descendants()
    .Attributes(ns + "SelectThisValues")
    .Select(x => x.Value);
相关问题