根据另一个元素值选择XML元素

时间:2021-09-25 15:33:27
<Root>
    <Sub>
        <Name>a</Name>
        <Value>1</Value>
    </Sub>
    <Sub>
        <Name>b</Name>
        <Value>2</Value>
    </Sub>
</Root>

How do I select the value of the Value element dependent upon the Name element?

如何选择依赖于Name元素的值元素的值?

Edit: In an XDocument, how do I get the value "1" when I have "a".

编辑:在XDocument中,当我有“a”时,如何获得值“1”。

4 个解决方案

#1


2  

I suggest you to use casting nodes instead of accessing Value property directly:

我建议您使用铸造节点,而不是直接访问Value property:

int value = xdoc.Descendants("Sub")
                .Where(s => (string)s.Element("Name") == "a")
                .Select(s => (int)s.Element("Value"))
                .FirstOrDefault();

If default value (zero) for missing nodes does not fit your needs, then you can check required Sub element exists before getting value:

如果缺失节点的默认值(0)不符合您的需要,那么您可以在获取值之前检查所需的子元素是否存在:

var sub = xdoc.Descendants("Sub")
              .FirstOrDefault(s => (string)s.Element("Name") == "a");

if (sub != null)            
    value = (int)sub.Element("Value");

Or simple one line with XPath and Linq:

或使用XPath和Linq的简单一行:

int value = (int)xdoc.XPathSelectElement("//Sub[Name='a']/Value");

#2


1  

Well, think about it...

嗯,想想……

you can easily read XML file, just you have to check the condition if the inner text of <Name> is match with your condition than you have to read the value of <value> tag.

您可以轻松读取XML文件,只需检查 的内部文本是否与您的条件匹配,而不必读取 标记的值。

Here is you can get answer for how to read XML file from c# code.

下面是如何从c#代码读取XML文件的答案。

#3


1  

you may try this, may help

你可以试试这个,可能会有帮助

var results = from row in xdoc.Root.Descendants("Sub")
where row.Element("Name").value ="value"
select new XElement("row", row.Element("Value"));

#4


1  

This should do it:

这应该这样做:

(assuming doc is an instance of XDocument)

(假设doc是XDocument的一个实例)

string name = "a";
var items = doc.Descendants("Sub")
               .Where(s => (string)s.Element("Name") == name)
               .Select(s => s.Element("Value").Value);

items would result as an IEnumerable<string> in this case.

在这种情况下,项将作为IEnumerable

If you know you only want one value:

如果你知道你只想要一个值:

string name = "a";
string value = doc.Descendants("Sub")
               .Where(s => (string)s.Element("Name") == name)
               .Select(s => s.Element("Value").Value)
               .FirstOrDefault();

#1


2  

I suggest you to use casting nodes instead of accessing Value property directly:

我建议您使用铸造节点,而不是直接访问Value property:

int value = xdoc.Descendants("Sub")
                .Where(s => (string)s.Element("Name") == "a")
                .Select(s => (int)s.Element("Value"))
                .FirstOrDefault();

If default value (zero) for missing nodes does not fit your needs, then you can check required Sub element exists before getting value:

如果缺失节点的默认值(0)不符合您的需要,那么您可以在获取值之前检查所需的子元素是否存在:

var sub = xdoc.Descendants("Sub")
              .FirstOrDefault(s => (string)s.Element("Name") == "a");

if (sub != null)            
    value = (int)sub.Element("Value");

Or simple one line with XPath and Linq:

或使用XPath和Linq的简单一行:

int value = (int)xdoc.XPathSelectElement("//Sub[Name='a']/Value");

#2


1  

Well, think about it...

嗯,想想……

you can easily read XML file, just you have to check the condition if the inner text of <Name> is match with your condition than you have to read the value of <value> tag.

您可以轻松读取XML文件,只需检查 的内部文本是否与您的条件匹配,而不必读取 标记的值。

Here is you can get answer for how to read XML file from c# code.

下面是如何从c#代码读取XML文件的答案。

#3


1  

you may try this, may help

你可以试试这个,可能会有帮助

var results = from row in xdoc.Root.Descendants("Sub")
where row.Element("Name").value ="value"
select new XElement("row", row.Element("Value"));

#4


1  

This should do it:

这应该这样做:

(assuming doc is an instance of XDocument)

(假设doc是XDocument的一个实例)

string name = "a";
var items = doc.Descendants("Sub")
               .Where(s => (string)s.Element("Name") == name)
               .Select(s => s.Element("Value").Value);

items would result as an IEnumerable<string> in this case.

在这种情况下,项将作为IEnumerable

If you know you only want one value:

如果你知道你只想要一个值:

string name = "a";
string value = doc.Descendants("Sub")
               .Where(s => (string)s.Element("Name") == name)
               .Select(s => s.Element("Value").Value)
               .FirstOrDefault();