使用 LINQ 从 XML 中提取属性

发布于 2024-10-03 15:19:39 字数 723 浏览 4 评论 0原文

我有以下 XML 文件(实际上是 VS2010 dbproj 文件),

<?xml version="1.0" enconding="utf-8"?>
<Project.....>
<propertyGroup>
....
</PropertyGroup>
<ItemGroup>
 <Build Include = "Schema Objects\Schemas\dbo\Programmability\Stored Procedures\foo.sql>
 </Build>
</ItemGroup>
</Project>

我想使用 LINQ to XML 提取作为存储过程的所有构建元素。 我有以下代码,但似乎不起作用:

var doc = XDocument.Load(filePath);
var elements = doc.Descendants("Build").Where( x => x.Attribute("Include").Value.Contains("Stored Procedure")).ToList();

What is the right way of extract the attribute value?

感谢您的回复!结果发现,Project 标签中指定了一个命名空间,但我省略了。这就是为什么我得到 0 结果的原因。

I have the following XML file (it's actually VS2010 dbproj file)

<?xml version="1.0" enconding="utf-8"?>
<Project.....>
<propertyGroup>
....
</PropertyGroup>
<ItemGroup>
 <Build Include = "Schema Objects\Schemas\dbo\Programmability\Stored Procedures\foo.sql>
 </Build>
</ItemGroup>
</Project>

I would like to use LINQ to XML to extract all Build elements that are Stored Procedures.
I have the following code, which doesn't seem to work:

var doc = XDocument.Load(filePath);
var elements = doc.Descendants("Build").Where( x => x.Attribute("Include").Value.Contains("Stored Procedure")).ToList();

What is the right way of extracting the attribute values?

Thanks for the replies! It turned out that there was a namespace specified in the Project tag which I omitted. That's why I was getting 0 results back.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-10-10 15:19:39
var doc = XElement.Parse(xml);
// or
var doc = XDocument.Load(path);

var q = from e in doc.Descendants("Build")
        from a in e.Attributes("Include")
        where a.Value.Contains("Stored Procedure")
        select e;

var list = q.ToList();

PS这种方法不需要检查每个变量是否为空,例如:

var q = from e in doc.Descendants("Build")
        where e != null
        from a in e.Attributes("Include")
        where a != null && a.Value != null && a.Value.Contains("Stored Procedure")
        select e;
var doc = XElement.Parse(xml);
// or
var doc = XDocument.Load(path);

var q = from e in doc.Descendants("Build")
        from a in e.Attributes("Include")
        where a.Value.Contains("Stored Procedure")
        select e;

var list = q.ToList();

P.S. this approach doesn't require check against null every variable, e.g.:

var q = from e in doc.Descendants("Build")
        where e != null
        from a in e.Attributes("Include")
        where a != null && a.Value != null && a.Value.Contains("Stored Procedure")
        select e;
北笙凉宸 2024-10-10 15:19:39

一旦修复了 XML(缺少结束引号,并且那里有不匹配的标签),它就可以正常工作。在 LINQPad 中测试

string xml = 
    "<Project>"+
    "<PropertyGroup>" +
    "</PropertyGroup>" +
    "<ItemGroup>" +
    "<Build Include = \"Schema Objects\\Schemas\\dbo\\Programmability\\Stored Procedures\\foo.sql\">"+
    "</Build>"+
    "</ItemGroup>"+
    "</Project>";

var doc = XDocument.Parse(xml);
var elements = doc.Descendants("Build").Where (x => x.Attribute("Include").Value.Contains("Stored Procedure")).ToList();
elements.Dump();

结果是:

<Build Include="Schema Objects\Schemas\dbo\Programmability\Stored Procedures\foo.sql">    </Build>

如果您只是想获取该值,请尝试以下操作:

var element2 = doc.Descendants("Build").Where (x => x.Attribute("Include").Value.Contains("Stored Procedure")).Select (x => x.Attribute("Include").Value);
element2.Dump();

编辑 - 这也有效:

var element3 = doc.Descendants("Build").Select(x=>x.Attribute("Include")).Where(y=>y.Value.Contains("Stored Procedure")).FirstOrDefault().Value;

Once you fix the XML (you're missing a closing quote, and you have unmatched tags there), it works fine. Tested in LINQPad

string xml = 
    "<Project>"+
    "<PropertyGroup>" +
    "</PropertyGroup>" +
    "<ItemGroup>" +
    "<Build Include = \"Schema Objects\\Schemas\\dbo\\Programmability\\Stored Procedures\\foo.sql\">"+
    "</Build>"+
    "</ItemGroup>"+
    "</Project>";

var doc = XDocument.Parse(xml);
var elements = doc.Descendants("Build").Where (x => x.Attribute("Include").Value.Contains("Stored Procedure")).ToList();
elements.Dump();

Result is:

<Build Include="Schema Objects\Schemas\dbo\Programmability\Stored Procedures\foo.sql">    </Build>

If you are just trying to get the value, try this:

var element2 = doc.Descendants("Build").Where (x => x.Attribute("Include").Value.Contains("Stored Procedure")).Select (x => x.Attribute("Include").Value);
element2.Dump();

EDIT - This works too:

var element3 = doc.Descendants("Build").Select(x=>x.Attribute("Include")).Where(y=>y.Value.Contains("Stored Procedure")).FirstOrDefault().Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文