11static 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 {
0 commit comments