无法使用XDocument创建WiX xml定义,因为它无法创建非法元素;`<! - ?include ..?/ - >`

时间:2013-10-17 16:38:40

标签: xml wix linq-to-xml

我想以编程方式创建下面的xml结构

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <?include "mypath\Constants.wxi"?>
    <Product Id="*" Name="$(var.ProductName)" Language="$(var.ProductLanguage)" Version="$(var.ProductVersion)" Manufacturer="$(var.ProductManufacturer)" UpgradeCode="$(var.ProductUpgradeCode)">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
        <MajorUpgrade DowngradeErrorMessage="A newer vesrion of [ProductName] is already installed" />
        <UIRef Id="WixUI_Minimal" />
        <MediaTemplate />
    </Product>
</Wix>

我尝试使用XDocument

  XDocument xmldoc = new XDocument(
        new XElement("Wix",
            new XText("<?include \"" + constantfileloc + "\"?>"),
            new XElement("Product",
              new XAttribute("Id", "*"),
              new XAttribute("Name", "$(var.ProductName)"),
              new XAttribute("Language", "$(var.ProductLanguage)"),
              new XAttribute("Version", "$(var.ProductVersion)"),
              new XAttribute("Manufacturer", "$(var.ProductManufacturer)"),
              new XAttribute("UpgradeCode", "$(var.ProductUpgradeCode)"),
              new XElement("Package",
                  new XAttribute("InstallerVersion", "200"),
                  new XAttribute("Compressed", "yes"),
                  new XAttribute("InstallScope", "perMachine")
              ),
              new XElement("MajorUpgrade",
                    new XAttribute("DowngradeErrorMessage", "A newer vesrion of [ProductName] is already installed")
              ),
              new XElement("UIRef",
                  new XAttribute("Id", "WixUI_Minimal")
              ),
              new XElement("MediaTemplate")
           )
        )
  );

问题是<?include?/>无法由XElement创建的,因为它不允许元素以?开头? 所以我认为我可以使用XText使其工作,但在<>的{​​{1}}声明中,它可以替换我的XText&lt }。

我也尝试过使用&gt 但我无法让它发挥作用,

是否可以使用XDocument?因为我真的不想使用文本作者

1 个答案:

答案 0 :(得分:3)

是的,这是可能的。你所看到的不是文字部分。他们正在处理指令。您必须使用XProcessingInstruction而不是XText。查看the documentation on the MSDN site。您可以使用类似于以下内容:

XDocument xmldoc = new XDocument(
    new XElement("Wix",
        new XProcessingInstruction("include", constantfileloc),
        new XElement("Product",
          new XAttribute("Id", "*"),
          new XAttribute("Name", "$(var.ProductName)"),
          new XAttribute("Language", "$(var.ProductLanguage)"),
          new XAttribute("Version", "$(var.ProductVersion)"),
          new XAttribute("Manufacturer", "$(var.ProductManufacturer)"),
          new XAttribute("UpgradeCode", "$(var.ProductUpgradeCode)"),
          new XElement("Package",
              new XAttribute("InstallerVersion", "200"),
              new XAttribute("Compressed", "yes"),
              new XAttribute("InstallScope", "perMachine")
          ),
          new XElement("MajorUpgrade",
                new XAttribute("DowngradeErrorMessage", "A newer vesrion of [ProductName] is already installed")
          ),
          new XElement("UIRef",
              new XAttribute("Id", "WixUI_Minimal")
          ),
          new XElement("MediaTemplate")
       )
    )
);