2012-03-29 134 views
0

提供以下XML我试图使用Linq to XML来分别更新UpgradeImage和TargetImage SourceFile属性。这个XML是如何形成的,或者我完全错过了什么?LINQ TO XML更新WIX补丁文件

<?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> 
+1

我对WiX一无所知,但没有看到任何代码很难说出什么问题。 – 2012-03-29 21:10:49

回答

1

我猜你忘了提供命名空间查询

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"); 

编辑时

另一种方法是使用XPathSelectElement扩展方法

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"; 
1

使用these xml extensions尝试,

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

扩展将自动为您获取名称空间。 Set()返回属性被设置的元素的XElement。所以第二个Set()从UpgradeImage元素开始。