在节点中获取多个元素的值几乎没有问题。 [英] Having little trouble getting the value of more than one element in a node.
本文介绍了在节点中获取多个元素的值几乎没有问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它仅适用于一个元素,但是当我尝试显示多个元素时,我什么也得不到。
XDocument doc = XDocument.Load( File.xml);
var 结果=(来自 c in doc.Descendants( Book)
其中 c.Attribute( ID)。 == 234332
选择 new {name = c.Element( 名称)。值,
author = c.Element( 作者 ).Value
}
);
foreach ( var q in 结果)
{
MessageBox.Show(q.name + + q.author);
}
解决方案
你可以试试像这样。
var results = 来自 p 人员
其中!(来自 e < span class =code-keyword> in exclusions
select e.CompositeKey)。包含(p.CompositeKey)
选择 p;
或
列表< TeamMember > teamMember =(来自db.TeamMembers中的tm
其中tm.MemberId == memberId
select tm).ToList < TeamMember > ();
类似解决方案:
string groupName = GroupB;
var items = 来自 g e.Elements(groupName)
来自 i g.Elements( 项目)
选择 new {
attr1 =( string )i.Attribute( attrib1),
attr2 =( string )i.Attribute ( attrib2)
};
foreach ( var item in items)
{
Console.WriteLine(item.attr1 + : + item.attr2);
}
谢谢
感谢大家的回复。这实际上是我的错,我错误输入了ID值。这就是为什么它不起作用。
这是解决方案。
< pre lang =c#> private static void ShowValue()
{
XDocument doc = XDocument.Load( File.xml 跨度>);
var 结果=(来自 c in doc.Descendants( Book)
其中 c.Attribute( ID)。 == 23433
选择 new {name = c.Element( 名称)。值,作者= c.Element( 作者)。值} );
foreach ( var q in 结果)
{
MessageBox.Show(q.name + + q.author);
}
}
It works with just one element, but when I try to show more than one I get nothing.
XDocument doc = XDocument.Load("File.xml");
var Result = (from c in doc.Descendants("Book")
where c.Attribute("ID").Value == "234332"
select new { name = c.Element("Name").Value,
author = c.Element("Author").Value
}
);
foreach (var q in Result)
{
MessageBox.Show(q.name + " " + q.author);
}
解决方案
Hi,
U can try like this.
var results = from p in People where !(from e in exclusions select e.CompositeKey).Contains(p.CompositeKey) select p;
Or
List<TeamMember> teamMember = (from tm in db.TeamMembers where tm.MemberId == memberId select tm).ToList<TeamMember>();
Similar type solution:
string groupName = "GroupB"; var items = from g in e.Elements(groupName) from i in g.Elements("Item") select new { attr1 = (string)i.Attribute("attrib1"), attr2 = (string)i.Attribute("attrib2") }; foreach (var item in items) { Console.WriteLine(item.attr1 + ":" + item.attr2); }
Thanks
Thanks everyone for the responses. It actually was my fault I mistyped the ID value. That''s why it wasn''t working.
Here is the solution.
private static void ShowValue() { XDocument doc = XDocument.Load("File.xml"); var Result = (from c in doc.Descendants("Book") where c.Attribute("ID").Value == "23433" select new { name = c.Element("Name").Value, author = c.Element("Author").Value}); foreach (var q in Result) { MessageBox.Show(q.name + " " + q.author); } }
这篇关于在节点中获取多个元素的值几乎没有问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文