删除xmlns =””将Reference元素添加到csproj中时的属性 [英] Remove xmlns="" attribute when adding Reference element into csproj
问题描述
我尝试通过MSI安装程序在安装服务时以编程方式将dll的引用添加到csproj文件中。
I try to programmatic add the reference of dll into csproj file at time of installation service through the MSI Installer.
<Reference Include="TestProject">
<HintPath>..\..\TestProject.dll</HintPath>
</Reference>
我在添加节点的源代码行下面放入了
受保护的替代无效ProjectInstall.cs的OnAfterInstall(IDictionary savedState)
I put below line of source code of add node into
protected override void OnAfterInstall(IDictionary savedState)
of ProjectInstaller.cs
var refnode = xml.CreateElement("Reference");
var attribute = xml.CreateAttribute("Include", null);
attribute.Value = "TestProject";
refnode.Attributes.Append(attribute);
var hintPath = xml.CreateNode(XmlNodeType.Element, "HintPath", null);
hintPath.InnerText = "..\..\TestProject.dll";
refnode.AppendChild(hintPath);
xml.AppendChild(refnode);
xml.Save(file);
代码输出
<Reference Include="TestProject" xmlns="">
<HintPath>..\..\TestProject.dll</HintPath>
</Reference>
但是源代码中添加了 xmlns =
将更多属性添加到参考元素中。这段代码中有什么问题,我将如何删除 xmlns =
属性,因为csproj文件未采用自定义属性。
But the source code add xmlns=""
more attribute into Reference element. what is wrong in this code how I will remove xmlns=""
attribute because csproj file not take custom attribute.
推荐答案
C#项目文件的默认命名空间为 xmlns = http://schemas.microsoft.com/developer/msbuild/2003
。您没有指定任何名称空间,因此必须清除该名称空间才能添加所需的子代。设置正确的命名空间,您可能就不会有问题。
The default namespace for C# project files is xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
. You didn't specify any namespace so the namespace had to be cleared to be able to add the children you wanted. Set the right namespaces and you probably won't have a problem.
var ns = "http://schemas.microsoft.com/developer/msbuild/2003";
var refnode = xml.CreateElement("Reference", ns);
var attribute = xml.CreateAttribute("Include", ns);
attribute.Value = "TestProject";
refnode.Attributes.Append(attribute);
var hintPath = xml.CreateNode(XmlNodeType.Element, "HintPath", ns);
hintPath.InnerText = "..\..\TestProject.dll";
refnode.AppendChild(hintPath);
xml.AppendChild(refnode);
xml.Save(file);
这篇关于删除xmlns =””将Reference元素添加到csproj中时的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!