如何通过LINQ遍历xml中的属性

时间:2021-02-14 14:32:09

Please help me with the below mentioned scenario which has xml and i want the code in C# LINQ

请帮我解决下面提到的具有xml的场景,我想要C#LINQ中的代码

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <Countries>
    <Country name="India">
      <state id="1"> Tamilnadu</state>
      <state> Karnataka</state>
    </Country>
    <Country name="America">
      <state id="1"> WI</state>
      <state> AW </state>
    </Country>
    <Country name="Africa">
      <state id="1"> Melbourne</state>
      <state> sydney </state>
    </Country>
  </Countries>
</root>

How to fetch state with attribute id=1 through LINQ since i am able to fetch attribute name="India"? And how to give id=1 i mean numeric value without "1"

如何获取属性id = 1到LINQ的状态,因为我能够获取属性名称=“印度”?如何给id = 1我的意思是没有“1”的数值

4 个解决方案

#1


2  

You could do the following:

您可以执行以下操作:

The null check is important, as judging by your structure without that null check you'll get a NullReferenceException.

空检查很重要,因为根据您的结构判断没有null检查,您将获得NullReferenceException。

XDocument xml = XDocument.Load("yourFileLocation");

var items = document.Root.Descendants("state")
    .Where(s => s.Attribute("id") != null && s.Attribute("id").Value == "1")
    .ToList();

#2


2  

Try the following.

请尝试以下方法。

XDocument xml = XDocument.Load(file);

XElement state = xml.Root.Descendants("Country")
    .First(c => c.Attribute("name").Value == "India")
    .Descendants("state")
    .First(s => (int)s.Attribute("id") == 1);

Next time post what you have tried first, so we can help you with your code.

下次发布您先尝试过的内容,以便我们为您提供代码帮助。

Also I did this without null checks. If the values aren't found it will die on First(). Up to you to do your own safety checks.

我也做了这个没有空检查。如果找不到值,它将在First()上死亡。由您自己进行安全检查。

#3


2  

If you are using C# you could do something like:

如果您使用C#,您可以执行以下操作:

 XDocument document = XDocument.Load("filePath");

 var states = (from state in document.Root.Descendants("state")
               where state.Attribute("id") != null && state.Attribute("id").Value == "1" 
               select state).ToList();

#4


0  

I find existing answers too verbose and lengthy. Imagine what would happen if you were going to do a selection based on multiple attributes?

我发现现有的答案过于冗长冗长。想象一下,如果您要根据多个属性进行选择会发生什么?

The most compact and expressive at the same time solution is to use XPath extensions (from System.Xml.XPath namespace).

最紧凑和最具表现力的解决方案是使用XPath扩展(来自System.Xml.XPath命名空间)。

For instance, to get state with id=1 in India:

例如,要在印度获得id = 1的州:

var xdoc = XDocument.Load(file);
foreach (var element in xdoc.XPathSelectElements("//Country[@name='India']/state[@id=1]"))
{
    Console.WriteLine("State " + element.Value + ", id " + (int)element.Attribute("id"));
}

To get all states in all countries that have any id assigned:

要获得分配了任何ID的所有国家/地区的所有州:

foreach (var element in xdoc.XPathSelectElements("//state[@id]"))
{
    Console.WriteLine("State " + element.Value + ", id " + (int)element.Attribute("id"));
}

Etc.

You can find XPath specification here.

你可以在这里找到XPath规范。

#1


2  

You could do the following:

您可以执行以下操作:

The null check is important, as judging by your structure without that null check you'll get a NullReferenceException.

空检查很重要,因为根据您的结构判断没有null检查,您将获得NullReferenceException。

XDocument xml = XDocument.Load("yourFileLocation");

var items = document.Root.Descendants("state")
    .Where(s => s.Attribute("id") != null && s.Attribute("id").Value == "1")
    .ToList();

#2


2  

Try the following.

请尝试以下方法。

XDocument xml = XDocument.Load(file);

XElement state = xml.Root.Descendants("Country")
    .First(c => c.Attribute("name").Value == "India")
    .Descendants("state")
    .First(s => (int)s.Attribute("id") == 1);

Next time post what you have tried first, so we can help you with your code.

下次发布您先尝试过的内容,以便我们为您提供代码帮助。

Also I did this without null checks. If the values aren't found it will die on First(). Up to you to do your own safety checks.

我也做了这个没有空检查。如果找不到值,它将在First()上死亡。由您自己进行安全检查。

#3


2  

If you are using C# you could do something like:

如果您使用C#,您可以执行以下操作:

 XDocument document = XDocument.Load("filePath");

 var states = (from state in document.Root.Descendants("state")
               where state.Attribute("id") != null && state.Attribute("id").Value == "1" 
               select state).ToList();

#4


0  

I find existing answers too verbose and lengthy. Imagine what would happen if you were going to do a selection based on multiple attributes?

我发现现有的答案过于冗长冗长。想象一下,如果您要根据多个属性进行选择会发生什么?

The most compact and expressive at the same time solution is to use XPath extensions (from System.Xml.XPath namespace).

最紧凑和最具表现力的解决方案是使用XPath扩展(来自System.Xml.XPath命名空间)。

For instance, to get state with id=1 in India:

例如,要在印度获得id = 1的州:

var xdoc = XDocument.Load(file);
foreach (var element in xdoc.XPathSelectElements("//Country[@name='India']/state[@id=1]"))
{
    Console.WriteLine("State " + element.Value + ", id " + (int)element.Attribute("id"));
}

To get all states in all countries that have any id assigned:

要获得分配了任何ID的所有国家/地区的所有州:

foreach (var element in xdoc.XPathSelectElements("//state[@id]"))
{
    Console.WriteLine("State " + element.Value + ", id " + (int)element.Attribute("id"));
}

Etc.

You can find XPath specification here.

你可以在这里找到XPath规范。