Skip to content

Commit 7514744

Browse files
committed
.
1 parent f6469bf commit 7514744

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/DeterministicIoPackaging/Relationships.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
static class Relationships
22
{
3-
static XName relationshipName = XName.Get("Relationship", "http://schemas.openxmlformats.org/package/2006/relationships");
4-
53
public static bool IsWorkbookRelationships(this Entry entry) =>
64
entry.FullName is "xl/_rels/workbook.xml.rels";
75

@@ -11,7 +9,7 @@ public static bool IsRelationships(this Entry entry) =>
119
public static XDocument PatchRelationships(Stream sourceStream)
1210
{
1311
var xml = XDocument.Load(sourceStream);
14-
PatchRelationships(xml, true);
12+
PatchRelationships(xml);
1513
return xml;
1614
}
1715

@@ -22,29 +20,34 @@ public static XDocument PatchWorkbookRelationships(Stream sourceStream)
2220
return xml;
2321
}
2422

25-
public static void PatchWorkbookRelationships(XDocument xml) =>
26-
PatchRelationships(xml, false);
23+
public static void PatchWorkbookRelationships(XDocument xml)
24+
{
25+
var root = xml.Root!;
26+
var relationships = root.Elements()
27+
.OrderBy(_ => _.Attribute("Type")!.Value)
28+
.ToList();
29+
root.ReplaceAll(relationships);
30+
}
2731

28-
public static void PatchRelationships(XDocument xml, bool patchIds)
32+
public static void PatchRelationships(XDocument xml)
2933
{
3034
var root = xml.Root!;
31-
var relationships = root.Elements(relationshipName)
32-
.Where(_ => !IsPsmdcpElement(_))
35+
var relationships = root.Elements()
3336
.OrderBy(_ => _.Attribute("Type")!.Value)
3437
.ToList();
3538

36-
root.Elements(relationshipName).Remove();
39+
foreach (var element in relationships.Where(IsPsmdcpElement).ToList())
40+
{
41+
relationships.Remove(element);
42+
}
3743

38-
if (patchIds)
44+
for (var index = 0; index < relationships.Count; index++)
3945
{
40-
for (var index = 0; index < relationships.Count; index++)
41-
{
42-
var relationship = relationships[index];
43-
relationship.Attribute("Id")!.SetValue($"DeterministicId{index + 1}");
44-
}
46+
var relationship = relationships[index];
47+
relationship.Attribute("Id")!.SetValue($"DeterministicId{index + 1}");
4548
}
4649

47-
root.Add(relationships);
50+
root.ReplaceAll(relationships);
4851

4952
static bool IsPsmdcpElement(XElement element)
5053
{

src/Tests/RelationshipsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Task Run()
2525
Target="package/services/metadata/core-properties/f81c51cec5594be694368ed6b7beba9.psmdcp"/>
2626
</Relationships>
2727
""");
28-
Relationships.PatchRelationships(xml, true);
28+
Relationships.PatchRelationships(xml);
2929

3030
return Verify(xml);
3131
}

0 commit comments

Comments
 (0)