LINQ TO XML Update WIX PATCH File

2019-08-20 08:11发布

Give the following XML I'm trying to update the UpgradeImage and TargetImage SourceFile attributes respectively using Linq to XML. Is there an issue with the way this XML is formed or am I just completely missing something?

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<PatchCreation
  Id="224C316C-5894-4771-BABF-21A3AC1F75FF"
  CleanWorkingFolder="yes"
  OutputPath="patch.pcp"
  WholeFilesOnly="yes">
<PatchInformation
    Description="Update Patch"
    Comments="Update Patch"
    ShortNames="no"
    Languages="1033"
    Compressed="yes"
    Manufacturer="me"/>

<PatchMetadata
    AllowRemoval="yes"
    Description="Update Patch"
    ManufacturerName="me"
    TargetProductName="Update"
    MoreInfoURL="http://andrewherrick.com/"
    Classification="Update"
    DisplayName="Update Patch"/>

<Family DiskId="5000"
    MediaSrcProp="Sample"
    Name="Update"
    SequenceStart="5000">
  <UpgradeImage SourceFile="c:\new.msi" Id="PatchUpgrade">
    <TargetImage SourceFile="c:\old.msi" Order="2" Id="PatchUpgrade" IgnoreMissingFiles="no" />
  </UpgradeImage>
</Family>

<PatchSequence PatchFamily="SamplePatchFamily"
    Sequence="1.0.0.0"
    Supersede="yes" />
</PatchCreation>
</Wix>

2条回答
劫难
2楼-- · 2019-08-20 08:15

I'm guessing you forgot to supply the namespace when querying

XNamespace ns = "http://schemas.microsoft.com/wix/2006/wi";

var doc = XDocument.Load(@"C:\test.xml");
var ui = doc.Elements(ns + "Wix").Elements(ns + "PatchCreation").
                 Elements(ns + "Family").Elements(ns + "UpgradeImage").Single ();

ui.Attribute("SourceFile" ).Value = "c:\newer.msi";

doc.Save(@"C:\test2.xml");

Edit

An alternative is to use the XPathSelectElement extension method

XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable()); 
mgr.AddNamespace("ns", "http://schemas.microsoft.com/wix/2006/wi"); 
var el = doc.Root.XPathSelectElement("//ns:Wix/ns:PatchCreation/ns:Family/ns:UpgradeImage", mgr);
el.Attribute("SourceFile").Value = @"c:\evennewer.msi";
查看更多
Anthone
3楼-- · 2019-08-20 08:35

Using these xml extensions try,

XElement wix = XElement.Load("file");
wix.Set("PatchCreation/Family/UpgradeImage/SourceFile", "new file path", true)
   .Set("TargetImage/SourceFile", "new file path", true);

The extensions will automatically get the namespace for you. Set() returns the XElement of the element that the attribute was set on. So the second Set() starts from the UpgradeImage element.

查看更多
登录 后发表回答