C# XDocument 通过其 name 属性的值获取 XML 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14504057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XDocument get XML element by the value of its name attribute
提问by kbaccouche
I have an XML
result like this
我有这样的XML
结果
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">16</int>
</lst>
<result name="response" numFound="3" start="0" maxScore="1.0">
<doc>
<str name="ContaFirstname">
firstname1
</str>
<str name="ContaId">6557</str>
<str name="ContaJobTitle">Manager</str>
<str name="ContaSurname">surname1
</str>
</doc>
<doc>
<str name="ContaFirstname">firstname2</str>
<str name="ContaId">6203</str>
<str name="ContaJobTitle">Director</str>
<str name="ContaSurname">surname2</str>
</doc>
</result>
</response>
I want to get a list of objects, and every object will contain the value of ContaFirstname
, ContaId
, ContaJobTitle
and ContaSurname
我想获得对象的列表,每个对象将包含的价值ContaFirstname
,ContaId
,ContaJobTitle
和ContaSurname
I tried something like this, but that's not right because I get them all NULL
我试过这样的事情,但那是不对的,因为我把它们都设为 NULL
var test = from c in xml.Descendants("doc")
select new
{
firstname = c.Element("ContaFirstname"),
surnmane = c.Element("ContaSurname")
};
So how can access these elements by name?
那么如何通过名称访问这些元素呢?
采纳答案by Jon Skeet
You don't want to access the elements by nameas most people would interpret that statement. You want to access the elements by the value of their name
attribute:
您不想按名称访问元素,因为大多数人会解释该语句。您想通过元素的name
属性值访问元素:
firstname = (string) c.Elements("str")
.First(x => x.Attribute("name").Value == "ContaFirstname");
//etc
You may well want to abstract that into a separate method, as it's going to be a pain to do it multiple times. For example:
您可能希望将其抽象为一个单独的方法,因为多次执行它会很痛苦。例如:
public static XElement ElementByNameAttribute(this XContainer container,
string name)
{
return container.Elements("str")
.First(x => x.Attribute("name").Value == name);
}
Then:
然后:
var test = from c in xml.Descendants("doc")
select new
{
firstname = c.ElementByNameAttribute("ContaFirstname").Value,
surnmane = c.ElementByNameAttribute("ContaSurname").Value
};
If you have any chance to give your document a more sensible structure, that would be preferable...
如果您有机会为您的文档提供更合理的结构,那将是更可取的......
回答by Cédric Bignon
Does this solve your problem:
这是否解决了您的问题:
var test = from c in xml.Descendants("doc")
select new
{
firstname = c.Elements("str").First(element => element.Attribute("name").Value == "ContaFirstname"),
surnmane = c.Elements("str").First(element => element.Attribute("name").Value == "ContaSurname")
};
or, if you want the values (instead of XElement
:
或者,如果您想要这些值(而不是XElement
:
var test = from c in xml.Descendants("doc")
select new
{
firstname = c.Elements("str").First(element => element.Attribute("name").Value == "ContaFirstname").Value,
surnmane = c.Elements("str").First(element => element.Attribute("name").Value == "ContaSurname").Value
};