通过xml方式更改项目文件的元素(changing an element of project file by xml means)

编程入门 行业动态 更新时间:2024-10-26 18:16:48
通过xml方式更改项目文件的元素(changing an element of project file by xml means)

有时我必须将最多60个winforms或类库项目加载到解决方案中..并更改每个项目的输出路径和参考路径。

所以我为此写了一个wpf应用程序

private void Button_Click(object sender, RoutedEventArgs e) { var path = txtRootPath.Text; var projFiles = System.IO.Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories); foreach (var item in projFiles) { var xDoc = XDocument.Load(item); var outputNodes = xDoc.Root.Descendants("OutputPath"); foreach (var outoutNode in outputNodes) { //this part is never hit.. outoutNode.Value = txtOutputPath.Text; } //similarly for referencePath } lblResult.Content = string.Format("Files: {0}", projFiles.Count()); }

但是outputNodes集合将为空

有人可以告诉我这里做错了什么

编辑:

我发现问题在于Project元素中的xmlns="http://schemas.microsoft.com/developer/msbuild/2003"属性。

解:

如此解决方案中给出的 - 将Visual Studio项目文件解析为XML 具有XDocument命名空间问题的Linq-to-XML

some times I will have to load up to 60 winforms or class library projects into a solution.. and change the output path and reference path for each of them..

so I wrote a wpf application for the same

private void Button_Click(object sender, RoutedEventArgs e) { var path = txtRootPath.Text; var projFiles = System.IO.Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories); foreach (var item in projFiles) { var xDoc = XDocument.Load(item); var outputNodes = xDoc.Root.Descendants("OutputPath"); foreach (var outoutNode in outputNodes) { //this part is never hit.. outoutNode.Value = txtOutputPath.Text; } //similarly for referencePath } lblResult.Content = string.Format("Files: {0}", projFiles.Count()); }

but outputNodes collection will be empty

could somebody please tell what am I doing wrong here

EDIT:

I figured out that the problem is with xmlns="http://schemas.microsoft.com/developer/msbuild/2003" attribute in Project element..

Solution:

as given in this solution - Parsing Visual Studio Project File as XML Linq-to-XML with XDocument namespace issue

最满意答案

引用“OutputPath”元素时,必须使用默认命名空间。

XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; var outputNodes = xDoc.Root.Descendants(ns + "OutputPath");

You have to use the default namespace when referring to "OutputPath" element.

XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; var outputNodes = xDoc.Root.Descendants(ns + "OutputPath");

更多推荐

本文发布于:2023-07-24 19:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   方式   文件   项目   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!