Xml 命名空间和 C# csproj [英] Xml namespace and C# csproj

查看:71
本文介绍了Xml 命名空间和 C# csproj的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 powershell 2.0 来编辑很多 csproj 文件.编辑的要求之一是添加不同条件的新PropertyGroup(请查看下面的示例)

I'm using powershell 2.0 to edit a lot of csproj files. One of the requirements for editing is to add new PropertyGroup with different condition (Please check the example below)

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'My-New-Env|AnyCPU'">

问题是powershell为我添加的所有新PropertyGroup标签添加了空的xmlns.

The problem is that powershell added the empty xmlns for all new PropertyGroup tags that I have added.

例如:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'My-New-Env|AnyCPU'" xmlns="">

有没有办法在没有任何命名空间的情况下添加新的 xml 节点?

在添加新的 PropertyGroup 之前,我尝试使用下面的代码删除命名空间属性,但它不起作用.(这意味着该属性实际上并未删除,添加新节点后我仍然可以看到空的命名空间.)

I tried removing the namespace attribute by using the code below before adding new PropertyGroup but it didn't work. (meaning that attribute is not actually removed and I can still see the empty namespace after adding new node.)

$content = [xml](gc $_.FullName);     

    Write-Host "Reading "$_.FullName -foregroundcolor yellow;

    $project = $content.Project;

    $content.Project.RemoveAttribute("xmlns");

我正在关注此帖子以添加新节点.

I'm following this post for adding new node.

如何从powershell向csproj添加新的PropertyGroup

示例:

$content = [xml](gc $_.FullName); 
  $importNode = $content.ImportNode($configs.DocumentElement, $true) 
  $project = $content.Project;
  $project
  $project.AppendChild($importNode);
  # $content.Save($_.FullName);

推荐答案

看这个帖子:http://bytes.com/topic/net/answers/377888-importing-nodes-without-namespace ,看来做起来不容易,可以的,但是有一个解决方法:

Looking at this thread: http://bytes.com/topic/net/answers/377888-importing-nodes-without-namespace, it seems that it can't be easily done, you can, however go with a workaround:

代替:

$content.Save($_.FullName);

使用:

$content = [xml] $content.OuterXml.Replace(" xmlns=`"`"", "")
$content.Save($_.FullName);

这篇关于Xml 命名空间和 C# csproj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆