您可以尝试这些 XElement 扩展方法:http ://searisen.com/xmllib/extensions.wiki
鉴于另一篇文章中的这个 xml,这是它的强大功能的一个示例:
<?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>
这将设置 UpgradeImage 标记的 SourceFile 属性的值以及 UpgradeImage 及其 SourceFile 中的 TargetImage 标记。
XElement wix = XElement.Load(xmlFile1.FullName);
wix.Set("PatchCreation/Family/UpgradeImage/SourceFile", "upgrade path", true)
.Set("TargetImage/SourceFile", "target path", true);
您也可以以相同的方式获取它们的值(无强制转换)。
string upgradeSource = wix.Get("PatchCreation/Family/UpgradeImage/SourceFile", string.Empty);
string targetSource = wix.Get("PatchCreation/Family/UpgradeImage/TargetImage/SourceFile", string.Empty);
或者这可以写成:
XElement upgradeImage = wix.GetElement("PatchCreation/Family/UpgradeImage");
string upgradeSource = upgradeImage.Get("SourceFile", string.Empty);
string targetSource = upgradeImage.Get("TargetImage/SourceFile", string.Empty);
要获取整数列表:
<root>
<path>
<list>
<value>1</value>
<value>12</value>
<value>13</value>
<value>14</value>
<value>15</value>
</list>
</path>
</root>
使用 GetEnumerable() 方法:
List<int> list = root
.GetEnumerable("path/list/value", xvalue => xvalue.Get(null, int.MinValue));
.ToList();
要设置一个新的整数列表:
var list2 = new int[] { 1, 3, 4, 5, 6, 7, 8, 9, 0 };
root.SetEnumerable("path/list", list2, a => new XElement("value", a));
这导致了这个新的xml:
<root>
<path>
<list>
<value>1</value>
<value>3</value>
<value>4</value>
<value>5</value>
<value>6</value>
<value>7</value>
<value>8</value>
<value>9</value>
<value>0</value>
</list>
</path>
</root>