C# Linq到XML文件查询不工作
这是我的XML文件,我正在使用linq和实体框架C# Linq到XML文件查询不工作,c#,asp.net-mvc,linq-to-xml,C#,Asp.net Mvc,Linq To Xml,这是我的XML文件,我正在使用linq和实体框架 <?xml version="1.0" encoding="utf-8" ?> <Projects> <Project ProjectId="JP001" Country="Canada" ProposedBy="Jim Priac"/> <Project ProjectId="KS12" Country="Canada" ProposedBy="Aya Suriour"/> <P
<?xml version="1.0" encoding="utf-8" ?>
<Projects>
<Project ProjectId="JP001" Country="Canada" ProposedBy="Jim Priac"/>
<Project ProjectId="KS12" Country="Canada" ProposedBy="Aya Suriour"/>
<Project ProjectId="ANS16" Country="Malesia" ProposedBy="Gour martin"/>
<Projects>
我使用的linq查询是
IEnumerable<string> Proposed_By = from project in xmldoc.Descendants("Projects").Elements("Project")
where project.Attribute("Country") == "Canada"
select project.Attribute("ProposedBy");
IEnumerable Proposed\u By=来自xmldoc.subjects(“Projects”).Elements(“project”)中的项目
其中project.Attribute(“国家”)=“加拿大”
选择project.Attribute(“ProposedBy”);
但是我没有得到正确的输出您需要与属性的值进行比较,而不是与属性本身进行比较
IEnumerable<string> Proposed_By =
from project in xmldoc.Descendants("Projects").Elements("Project")
where project.Attribute("Country").Value == "Canada"
select project.Attribute("ProposedBy").Value;
首先查看您的文件是否已被删除 对linq查询进行更正
XDocument xmldoc = XDocument.Load(Path.GetFullPath("filename"));
IEnumerable<string> Proposed_By = from project in xmldoc.Descendants("Projects").Elements("Project")
where project.Attribute("Country").Value == "Canada"
select project.Attribute("ProposedBy").Value;
xdocumentxmldoc=XDocument.Load(Path.GetFullPath(“文件名”);
IEnumerable Proposed_By=来自xmldoc.subjects(“项目”)元素(“项目”)中的项目
其中project.Attribute(“国家”).Value==“加拿大”
选择project.Attribute(“ProposedBy”).Value;
将.Value
与属性名一起使用另一种方法:
如果值不存在,则返回NULL
var Propposed_By = from project in xd.Descendants("Projects").Elements("Project")
where project.Attribute("Country").Value == "Canada"
select (string)project.Attribute("ProposedBy");
谢谢,但我没有得到输出。您如何加载xmldoc?它是XDocument还是XmlDocument?如果您还显示了加载代码,可能会有所帮助。@Jehof include Ya@V2Solutions-MS Team
var Propposed_By = from project in xd.Descendants("Projects").Elements("Project")
where project.Attribute("Country").Value == "Canada"
select (string)project.Attribute("ProposedBy");