有时我必须将最多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");更多推荐
发布评论