为了能够让用户自行部署ClickOnce应用程序,需要编写一个生成ClickOnce应用程序的ClickOnce专用安装程序setup.exe,而生成这个setup.exe的方法就是编写一个XML格式的生成配置文件,使用MSBuild.exe来创建。
  一般情况下,创建XML文件本来是个很简单的事情,用XDocument、XElement、XAttribute一顿Add,然后Save成文件就完成了。但是创建setup.exe用的XML文件的根节点(Project)必须指定XML名称空间"http://schemas.microsoft.com/developer/msbuild/2003",形式如下:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<BootstrapperFile Include=".NETFramework,Version=v4.0">
<ProductName>Microsoft .NET Framework 4 (x86 和 x64)</ProductName>
</BootstrapperFile>
<BootstrapperFile Include="Microsoft.Windows.Installer.3.1">
<ProductName>Windows Installer 3.1</ProductName>
</BootstrapperFile>
</ItemGroup> <Target Name="BuildBootstrapper">
<GenerateBootstrapper
ApplicationFile="MyApp.application"
ApplicationName="我的应用程序"
ApplicationUrl="http://192.168.0.8/MyApp/"
BootstrapperItems="@(BootstrapperFile)"
CopyComponents="false"
OutputPath="."
Path="X:\SerupExeBuilder\Packages"/>
</Target>
</Project>

于是,生成该XML的代码则写为:

#region ItemGroup
XElement itemGroup = new XElement("ItemGroup",
new XElement("BootstrapperFile",
new XAttribute("Include", ".NETFramework,Version=v4.0"),
new XElement("ProductName", "Microsoft .NET Framework 4 (x86 和 x64%)")),
new XElement("BootstrapperFile",
new XAttribute("Include", "Microsoft.Windows.Installer.3.1"),
new XElement("ProductName", "Windows Installer 3.1"))
);
#endregion #region Target
XElement target = new XElement("Target");
target.Add(new XAttribute("Name", "BuildBootstrapper"));
XElement generateBootstrapper = new XElement("GenerateBootstrapper");
generateBootstrapper.Add(new XAttribute("ApplicationFile", applicationFile));
generateBootstrapper.Add(new XAttribute("ApplicationName", applicationName));
generateBootstrapper.Add(new XAttribute("ApplicationUrl", applicationUrl)); if (componentsLocation == )
{
generateBootstrapper.Add(new XAttribute("ComponentsLocation", "Relative"));
}
else if (componentsLocation == && !string.IsNullOrWhiteSpace(componentsUrl))
{
generateBootstrapper.Add(new XAttribute("ComponentsLocation", "Absolute"));
generateBootstrapper.Add(new XAttribute("ComponentsUrl", componentsUrl));
} generateBootstrapper.Add(new XAttribute("BootstrapperItems", "@(BootstrapperFile)"));
generateBootstrapper.Add(new XAttribute("CopyComponents", false));
generateBootstrapper.Add(new XAttribute("OutputPath", outputPath));
string packagesPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
generateBootstrapper.Add(new XAttribute("Path", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));
target.Add(generateBootstrapper);
#endregion XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";
XElement root = new XElement(xmlns + "Project"); root.Add(itemGroup);
root.Add(target); XDocument setupDocument = new XDocument();
setupDocument.Add(root); setupDocument.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
setupDocument.Save(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "setup.xml"));

但是生成的XML文件中,ItemGroup和Target节点上会出现 xmlns="" 的属性,而这在编译的时候是不允许的(也是错误的),而又没有办法不让它们出现。

这个问题郁闷了一阵子,也没找到资料,最后用土办法,也就是StringBuilder手工创建,程序是成功了,但是这个问题一直耿耿于怀。

今天终于知道原因了,当然也知道了“解决办法”了。
其实原因是,在XML中,如果某个节点使用了NameSpace,则要求其下的每个节点都必须指定NameSpace的,以便于区分各个节点属于哪一个NameSpace。在生成XML文件的时候,如果子节点的NameSpace与父节点相同,则忽略xmlns属性。所以在创建子节点的时候,必须为子节点指定NameSpace,否则就会因为没有指定NameSpace,出现 xmlns="" 的情况。
所以是对XML的NameSpace了解的不够,才导致程序的错误写法。因此,改为正确的写法,问题自然就消失不见了。当然,这个实际上不能称为“解决办法” 。

另外,该setup.exe也可以使用Microsoft.Build.Tasks.GenerateBootstrapper生成,但是目前只做到生成,尚未进行实践验证。

解决创建带有NameSpace的XML文件出现空白xmlns的问题的更多相关文章

  1. 用python解决打标签时将xml文件的标签名打错

    用python解决打标签时将xml文件的标签名打错 问题描述:再进行达标签时将magnetic_tile的标签名错误的打成了magnetic_title,又不想一张一张的修改 出现问题的xml文件 & ...

  2. dom4j: 生成的XML文件根节点 xmlns="" 的问题

    使用dom4j写入XML文件时,写入完毕后发现root element中没有 xmlns,也即是没有命名空间. 正确的写法如下: Document document = DocumentHelper. ...

  3. 解决Android工程里的xml文件自动提示问题

    昨天晚上看某培训机构的Android的 视频教程,看到他在写布局的XML文件时,有很方便的自动提示功能.我就在自己的Eclipse里试了一下,可是我的没实现.就到网上查,很多都说:在 Window-& ...

  4. 解决Maven项目中pom.xml文件报错(Failure to transfer....)的问题

    打开pom.xml文件,查看错误,如果错误类型为:Failure to transfer.........之类的,则表明你的pom中依赖的jar包没有完全下载. 解决方法:找到你本地的maven仓库, ...

  5. 我来说说XML文件中的xmlns、xmlns:xsi和xsi:schemaLocation、dtd文件的具体含义

    文章摘自:https://yq.aliyun.com/articles/40353               http://www.cnblogs.com/zhao1949/p/5652167.ht ...

  6. 关于spring xml文件中的xmlns,xsi:schemaLocation(转)

    使用spring也有一段时间了,配置文件也见了不少了,但是发现配置文件的beans里面有很多链接,一开始也很迷惑,所以抽了一点时间整里了一下. 首先我们看到的一个spring的配置文件大概如下面这个样 ...

  7. 关于spring xml文件中的xmlns,xsi:schemaLocation

    链接:https://blog.csdn.net/u010571844/article/details/50767151 使用spring也有一段时间了,配置文件也见了不少了,但是发现配置文件的bea ...

  8. 创建SpringBoot项目pom.xml文件第一行报错:Non-parseable POM E:\maven\repository\org\springframework\securit

    在编辑pom.xml时,第一行有个刺眼红色×,然后在Problems看到这个问题 [ERROR] The build could not read 1 project -> [Help 1]E: ...

  9. idea 创建项目没有web.xml文件,如何添加

    1.首先看下项目工程里面是否有WEB-INF文件夹,没有就创建一个 2.点击 file 选择 project structure 3.选择 facets,点击+号, 选择 web 4.弹出 弹框 选择 ...

随机推荐

  1. 两数之和-数据结构设计 · Two Sum - Data structure design

    [抄题]: 设计b并实现一个 TwoSum 类.他需要支持以下操作:add 和 find.add -把这个数添加到内部的数据结构.find -是否存在任意一对数字之和等于这个值 [思维问题]: 不知道 ...

  2. AdmBaseController 判断是否登录

    代码 using Service.IService; using System; using System.Collections.Generic; using System.Linq; using ...

  3. CookiesHelper

    /// <summary> ///CookiesHelper 的摘要说明 /// </summary> public class CookiesHelper { public ...

  4. .NET Utils 辅助类

    using System;using System.Diagnostics;using System.IO;using System.Reflection;using System.Runtime.I ...

  5. 教你如何制作饼干icon教程

    Hello,不露又和大家见面了,今天给大家带来的是一个可爱Q弹的icon~ 看起来像块饼干是吧~ 做起来非常简单哦,快打开PS一起躁起来吧. 先来看看效果图: 步骤1:打开PS,新建一个800*600 ...

  6. Jmeter中控制某一段脚本失败后重复执行,并在每个HTTP Request名字中加上循环次数

    ================================================== 1.While Controller之前有一个BeanShell Sampler,用于Init N ...

  7. 启动多个eclipse 时,因为一个另一个启动报错,

    启动多个eclipse 时,因为一个另一个启动报错, 原因: 可能是 有一个 eclipse  中 的 tomcat  配置出错:preference中  tomcat 配置  context dec ...

  8. mysql 1045 access denied for user********

    另一个方法Windows: 1. 管理员登陆系统,停止mysql服务或者结束mysqld-nt进程2. 进入命令行,来到mysql的安装目录.假设安装目录为 d:/mysql/ , CMD进入命令行3 ...

  9. js函数在frame中的相互调用详解

    原文章:http://www.jb51.net/article/47557.htm   一个HTML页面可以有一个或多个子框架,这些子框架以<iframe>来标记,用来显示一个独立的HTM ...

  10. 2018.10.23 NOIP模拟 行星通道计划(bit)

    传送门 卡常题. 成功卡掉了作死写树套树的zxy. 然而对我的二维bit无能为力. 直接维护两棵bit. bit1[i][j]bit1[i][j]bit1[i][j]表示左端点小于等于iii,右端点小 ...