-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlHelper.cs
845 lines (802 loc) · 22.9 KB
/
XmlHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*******************************************************************************
* You may amend and distribute as you like, but don't remove this header!
*
* EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
* Copyright (C) 2011 Jan Källman
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
*
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
* Code change notes:
*
* Author Change Date
* ******************************************************************************
* Jan Källman Initial Release 2009-10-01
* Jan Källman License changed GPL-->LGPL 2011-12-27
* Eyal Seagull Add "CreateComplexNode" 2012-04-03
* Eyal Seagull Add "DeleteTopNode" 2012-04-13
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OfficeOpenXml.Style;
using System.Globalization;
using System.IO;
namespace OfficeOpenXml
{
/// <summary>
/// Help class containing XML functions.
/// Can be Inherited
/// </summary>
public abstract class XmlHelper
{
internal delegate int ChangedEventHandler(StyleBase sender, Style.StyleChangeEventArgs e);
internal XmlHelper(XmlNamespaceManager nameSpaceManager)
{
TopNode = null;
NameSpaceManager = nameSpaceManager;
}
internal XmlHelper(XmlNamespaceManager nameSpaceManager, XmlNode topNode)
{
TopNode = topNode;
NameSpaceManager = nameSpaceManager;
}
//internal bool ChangedFlag;
internal XmlNamespaceManager NameSpaceManager { get; set; }
internal XmlNode TopNode { get; set; }
string[] _schemaNodeOrder = null;
/// <summary>
/// Schema order list
/// </summary>
internal string[] SchemaNodeOrder
{
get
{
return _schemaNodeOrder;
}
set
{
_schemaNodeOrder = value;
}
}
internal XmlNode CreateNode(string path)
{
if (path == "")
return TopNode;
else
return CreateNode(path, false);
}
/// <summary>
/// Create the node path. Nodesa are inserted according to the Schema node oreder
/// </summary>
/// <param name="path">The path to be created</param>
/// <param name="insertFirst">Insert as first child</param>
/// <param name="addNew">Always add a new item at the last level.</param>
/// <returns></returns>
internal XmlNode CreateNode(string path, bool insertFirst, bool addNew=false)
{
XmlNode node = TopNode;
XmlNode prependNode = null;
if (path.StartsWith("/")) path = path.Substring(1);
var subPaths = path.Split('/');
for(int i= 0; i < subPaths.Length;i++)
{
string subPath = subPaths[i];
XmlNode subNode = node.SelectSingleNode(subPath, NameSpaceManager);
if (subNode == null || (i==subPath.Length-1 && addNew))
{
string nodeName;
string nodePrefix;
string nameSpaceURI = "";
string[] nameSplit = subPath.Split(':');
if (SchemaNodeOrder != null && subPath[0] != '@')
{
insertFirst = false;
prependNode = GetPrependNode(subPath, node);
}
if (nameSplit.Length > 1)
{
nodePrefix = nameSplit[0];
if (nodePrefix[0] == '@') nodePrefix = nodePrefix.Substring(1, nodePrefix.Length - 1);
nameSpaceURI = NameSpaceManager.LookupNamespace(nodePrefix);
nodeName = nameSplit[1];
}
else
{
nodePrefix = "";
nameSpaceURI = "";
nodeName = nameSplit[0];
}
if (subPath.StartsWith("@"))
{
XmlAttribute addedAtt = node.OwnerDocument.CreateAttribute(subPath.Substring(1, subPath.Length - 1), nameSpaceURI); //nameSpaceURI
node.Attributes.Append(addedAtt);
}
else
{
if (nodePrefix == "")
{
subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI);
}
else
{
if (nodePrefix == "" || (node.OwnerDocument != null && node.OwnerDocument.DocumentElement != null && node.OwnerDocument.DocumentElement.NamespaceURI == nameSpaceURI &&
node.OwnerDocument.DocumentElement.Prefix == ""))
{
subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI);
}
else
{
subNode = node.OwnerDocument.CreateElement(nodePrefix, nodeName, nameSpaceURI);
}
}
if (prependNode != null)
{
node.InsertBefore(subNode, prependNode);
prependNode = null;
}
else if (insertFirst)
{
node.PrependChild(subNode);
}
else
{
node.AppendChild(subNode);
}
}
}
node = subNode;
}
return node;
}
/// <summary>
/// Options to insert a node in the XmlDocument
/// </summary>
internal enum eNodeInsertOrder
{
/// <summary>
/// Insert as first node of "topNode"
/// </summary>
First,
/// <summary>
/// Insert as the last child of "topNode"
/// </summary>
Last,
/// <summary>
/// Insert after the "referenceNode"
/// </summary>
After,
/// <summary>
/// Insert before the "referenceNode"
/// </summary>
Before,
/// <summary>
/// Use the Schema List to insert in the right order. If the Schema list
/// is null or empty, consider "Last" as the selected option
/// </summary>
SchemaOrder
}
/// <summary>
/// Create a complex node. Insert the node according to SchemaOrder
/// using the TopNode as the parent
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
internal XmlNode CreateComplexNode(
string path)
{
return CreateComplexNode(
TopNode,
path,
eNodeInsertOrder.SchemaOrder,
null);
}
/// <summary>
/// Create a complex node. Insert the node according to the <paramref name="path"/>
/// using the <paramref name="topNode"/> as the parent
/// </summary>
/// <param name="topNode"></param>
/// <param name="path"></param>
/// <returns></returns>
internal XmlNode CreateComplexNode(
XmlNode topNode,
string path)
{
return CreateComplexNode(
topNode,
path,
eNodeInsertOrder.SchemaOrder,
null);
}
/// <summary>
/// Creates complex XML nodes
/// </summary>
/// <remarks>
/// 1. "d:conditionalFormatting"
/// 1.1. Creates/find the first "conditionalFormatting" node
///
/// 2. "d:conditionalFormatting/@sqref"
/// 2.1. Creates/find the first "conditionalFormatting" node
/// 2.2. Creates (if not exists) the @sqref attribute
///
/// 3. "d:conditionalFormatting/@id='7'/@sqref='A9:B99'"
/// 3.1. Creates/find the first "conditionalFormatting" node
/// 3.2. Creates/update its @id attribute to "7"
/// 3.3. Creates/update its @sqref attribute to "A9:B99"
///
/// 4. "d:conditionalFormatting[@id='7']/@sqref='X1:X5'"
/// 4.1. Creates/find the first "conditionalFormatting" node with @id=7
/// 4.2. Creates/update its @sqref attribute to "X1:X5"
///
/// 5. "d:conditionalFormatting[@id='7']/@id='8'/@sqref='X1:X5'/d:cfRule/@id='AB'"
/// 5.1. Creates/find the first "conditionalFormatting" node with @id=7
/// 5.2. Set its @id attribute to "8"
/// 5.2. Creates/update its @sqref attribute and set it to "X1:X5"
/// 5.3. Creates/find the first "cfRule" node (inside the node)
/// 5.4. Creates/update its @id attribute to "AB"
///
/// 6. "d:cfRule/@id=''"
/// 6.1. Creates/find the first "cfRule" node
/// 6.1. Remove the @id attribute
/// </remarks>
/// <param name="topNode"></param>
/// <param name="path"></param>
/// <param name="nodeInsertOrder"></param>
/// <param name="referenceNode"></param>
/// <returns>The last node creates/found</returns>
internal XmlNode CreateComplexNode(
XmlNode topNode,
string path,
eNodeInsertOrder nodeInsertOrder,
XmlNode referenceNode)
{
// Path is obrigatory
if ((path == null) || (path == string.Empty))
{
return topNode;
}
XmlNode node = topNode;
string nameSpaceURI = string.Empty;
//TODO: BUG: when the "path" contains "/" in an attrribue value, it gives an error.
// Separate the XPath to Nodes and Attributes
foreach (string subPath in path.Split('/'))
{
// The subPath can be any one of those:
// nodeName
// x:nodeName
// nodeName[find criteria]
// x:nodeName[find criteria]
// @attribute
// @attribute='attribute value'
// Check if the subPath has at least one character
if (subPath.Length > 0)
{
// Check if the subPath is an attribute (with or without value)
if (subPath.StartsWith("@"))
{
// @attribute --> Create attribute
// @attribute='' --> Remove attribute
// @attribute='attribute value' --> Create attribute + update value
string[] attributeSplit = subPath.Split('=');
string attributeName = attributeSplit[0].Substring(1, attributeSplit[0].Length - 1);
string attributeValue = null; // Null means no attribute value
// Check if we have an attribute value to set
if (attributeSplit.Length > 1)
{
// Remove the ' or " from the attribute value
attributeValue = attributeSplit[1].Replace("'", "").Replace("\"", "");
}
// Get the attribute (if exists)
XmlAttribute attribute = (XmlAttribute)(node.Attributes.GetNamedItem(attributeName));
// Remove the attribute if value is empty (not null)
if (attributeValue == string.Empty)
{
// Only if the attribute exists
if (attribute != null)
{
node.Attributes.Remove(attribute);
}
}
else
{
// Create the attribue if does not exists
if (attribute == null)
{
// Create the attribute
attribute = node.OwnerDocument.CreateAttribute(
attributeName);
// Add it to the current node
node.Attributes.Append(attribute);
}
// Update the attribute value
if (attributeValue != null)
{
node.Attributes[attributeName].Value = attributeValue;
}
}
}
else
{
// nodeName
// x:nodeName
// nodeName[find criteria]
// x:nodeName[find criteria]
// Look for the node (with or without filter criteria)
XmlNode subNode = node.SelectSingleNode(subPath, NameSpaceManager);
// Check if the node does not exists
if (subNode == null)
{
string nodeName;
string nodePrefix;
string[] nameSplit = subPath.Split(':');
nameSpaceURI = string.Empty;
// Check if the name has a prefix like "d:nodeName"
if (nameSplit.Length > 1)
{
nodePrefix = nameSplit[0];
nameSpaceURI = NameSpaceManager.LookupNamespace(nodePrefix);
nodeName = nameSplit[1];
}
else
{
nodePrefix = string.Empty;
nameSpaceURI = string.Empty;
nodeName = nameSplit[0];
}
// Check if we have a criteria part in the node name
if (nodeName.IndexOf("[") > 0)
{
// remove the criteria from the node name
nodeName = nodeName.Substring(0, nodeName.IndexOf("["));
}
if (nodePrefix == string.Empty)
{
subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI);
}
else
{
if (node.OwnerDocument != null
&& node.OwnerDocument.DocumentElement != null
&& node.OwnerDocument.DocumentElement.NamespaceURI == nameSpaceURI
&& node.OwnerDocument.DocumentElement.Prefix == string.Empty)
{
subNode = node.OwnerDocument.CreateElement(
nodeName,
nameSpaceURI);
}
else
{
subNode = node.OwnerDocument.CreateElement(
nodePrefix,
nodeName,
nameSpaceURI);
}
}
// Check if we need to use the "SchemaOrder"
if (nodeInsertOrder == eNodeInsertOrder.SchemaOrder)
{
// Check if the Schema Order List is empty
if ((SchemaNodeOrder == null) || (SchemaNodeOrder.Length == 0))
{
// Use the "Insert Last" option when Schema Order List is empty
nodeInsertOrder = eNodeInsertOrder.Last;
}
else
{
// Find the prepend node in order to insert
referenceNode = GetPrependNode(nodeName, node);
if (referenceNode != null)
{
nodeInsertOrder = eNodeInsertOrder.Before;
}
else
{
nodeInsertOrder = eNodeInsertOrder.Last;
}
}
}
switch (nodeInsertOrder)
{
case eNodeInsertOrder.After:
node.InsertAfter(subNode, referenceNode);
referenceNode = null;
break;
case eNodeInsertOrder.Before:
node.InsertBefore(subNode, referenceNode);
referenceNode = null;
break;
case eNodeInsertOrder.First:
node.PrependChild(subNode);
break;
case eNodeInsertOrder.Last:
node.AppendChild(subNode);
break;
}
}
// Make the newly created node the top node when the rest of the path
// is being evaluated. So newly created nodes will be the children of the
// one we just created.
node = subNode;
}
}
}
// Return the last created/found node
return node;
}
/// <summary>
/// return Prepend node
/// </summary>
/// <param name="nodeName">name of the node to check</param>
/// <param name="node">Topnode to check children</param>
/// <returns></returns>
private XmlNode GetPrependNode(string nodeName, XmlNode node)
{
int pos = GetNodePos(nodeName);
if (pos < 0)
{
return null;
}
XmlNode prependNode = null;
foreach (XmlNode childNode in node.ChildNodes)
{
int childPos = GetNodePos(childNode.Name);
if (childPos > -1) //Found?
{
if (childPos > pos) //Position is before
{
prependNode = childNode;
break;
}
}
}
return prependNode;
}
private int GetNodePos(string nodeName)
{
int ix = nodeName.IndexOf(":");
if (ix > 0)
{
nodeName = nodeName.Substring(ix + 1, nodeName.Length - (ix + 1));
}
for (int i = 0; i < _schemaNodeOrder.Length; i++)
{
if (nodeName == _schemaNodeOrder[i])
{
return i;
}
}
return -1;
}
internal void DeleteAllNode(string path)
{
string[] split = path.Split('/');
XmlNode node = TopNode;
foreach (string s in split)
{
node = node.SelectSingleNode(s, NameSpaceManager);
if (node != null)
{
if (node is XmlAttribute)
{
(node as XmlAttribute).OwnerElement.Attributes.Remove(node as XmlAttribute);
}
else
{
node.ParentNode.RemoveChild(node);
}
}
else
{
break;
}
}
}
internal void DeleteNode(string path)
{
var node = TopNode.SelectSingleNode(path, NameSpaceManager);
if (node != null)
{
if (node is XmlAttribute)
{
var att = (XmlAttribute)node;
att.OwnerElement.Attributes.Remove(att);
}
else
{
node.ParentNode.RemoveChild(node);
}
}
}
internal void DeleteTopNode()
{
TopNode.ParentNode.RemoveChild(TopNode);
}
internal void SetXmlNodeString(string path, string value)
{
SetXmlNodeString(TopNode, path, value, false, false);
}
internal void SetXmlNodeString(string path, string value, bool removeIfBlank)
{
SetXmlNodeString(TopNode, path, value, removeIfBlank, false);
}
internal void SetXmlNodeString(XmlNode node, string path, string value)
{
SetXmlNodeString(node, path, value, false, false);
}
internal void SetXmlNodeString(XmlNode node, string path, string value, bool removeIfBlank)
{
SetXmlNodeString(node, path, value, removeIfBlank, false);
}
internal void SetXmlNodeString(XmlNode node, string path, string value, bool removeIfBlank, bool insertFirst)
{
if (node == null)
{
return;
}
if (value == "" && removeIfBlank)
{
DeleteAllNode(path);
}
else
{
XmlNode nameNode = node.SelectSingleNode(path, NameSpaceManager);
if (nameNode == null)
{
CreateNode(path, insertFirst);
nameNode = node.SelectSingleNode(path, NameSpaceManager);
}
//if (nameNode.InnerText != value) HasChanged();
nameNode.InnerText = value;
}
}
internal void SetXmlNodeBool(string path, bool value)
{
SetXmlNodeString(TopNode, path, value ? "1" : "0", false, false);
}
internal void SetXmlNodeBool(string path, bool value, bool removeIf)
{
if (value == removeIf)
{
var node = TopNode.SelectSingleNode(path, NameSpaceManager);
if (node != null)
{
if (node is XmlAttribute)
{
var elem = (node as XmlAttribute).OwnerElement;
elem.ParentNode.RemoveChild(elem);
}
else
{
TopNode.RemoveChild(node);
}
}
}
else
{
SetXmlNodeString(TopNode, path, value ? "1" : "0", false, false);
}
}
internal bool ExistNode(string path)
{
if (TopNode == null || TopNode.SelectSingleNode(path, NameSpaceManager) == null)
{
return false;
}
else
{
return true;
}
}
internal bool? GetXmlNodeBoolNullable(string path)
{
var value = GetXmlNodeString(path);
if (string.IsNullOrEmpty(value))
{
return null;
}
return GetXmlNodeBool(path);
}
internal bool GetXmlNodeBool(string path)
{
return GetXmlNodeBool(path, false);
}
internal bool GetXmlNodeBool(string path, bool blankValue)
{
string value = GetXmlNodeString(path);
if (value == "1" || value == "-1" || value.Equals("true",StringComparison.OrdinalIgnoreCase))
{
return true;
}
else if (value == "")
{
return blankValue;
}
else
{
return false;
}
}
internal int GetXmlNodeInt(string path)
{
int i;
if (int.TryParse(GetXmlNodeString(path), NumberStyles.Number, CultureInfo.InvariantCulture, out i))
{
return i;
}
else
{
return int.MinValue;
}
}
internal int? GetXmlNodeIntNull(string path)
{
int i;
string s = GetXmlNodeString(path);
if (s!="" && int.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture, out i))
{
return i;
}
else
{
return null;
}
}
internal decimal GetXmlNodeDecimal(string path)
{
decimal d;
if (decimal.TryParse(GetXmlNodeString(path), NumberStyles.Any, CultureInfo.InvariantCulture, out d))
{
return d;
}
else
{
return 0;
}
}
internal decimal? GetXmlNodeDecimalNull(string path)
{
decimal d;
if (decimal.TryParse(GetXmlNodeString(path), NumberStyles.Any, CultureInfo.InvariantCulture, out d))
{
return d;
}
else
{
return null;
}
}
internal double? GetXmlNodeDoubleNull(string path)
{
string s = GetXmlNodeString(path);
if (s == "")
{
return null;
}
else
{
double v;
if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
{
return v;
}
else
{
return null;
}
}
}
internal double GetXmlNodeDouble(string path)
{
string s = GetXmlNodeString(path);
if (s == "")
{
return double.NaN;
}
else
{
double v;
if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
{
return v;
}
else
{
return double.NaN;
}
}
}
internal string GetXmlNodeString(XmlNode node, string path)
{
if (node == null)
{
return "";
}
XmlNode nameNode = node.SelectSingleNode(path, NameSpaceManager);
if (nameNode != null)
{
if (nameNode.NodeType == XmlNodeType.Attribute)
{
return nameNode.Value != null ? nameNode.Value : "";
}
else
{
return nameNode.InnerText;
}
}
else
{
return "";
}
}
internal string GetXmlNodeString(string path)
{
return GetXmlNodeString(TopNode, path);
}
internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri)
{
var id = 1;
return GetNewUri(package, sUri, ref id);
}
internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri, ref int id)
{
Uri uri = new Uri(string.Format(sUri, id), UriKind.Relative);
while (package.PartExists(uri))
{
uri = new Uri(string.Format(sUri, ++id), UriKind.Relative);
}
return uri;
}
/// <summary>
/// Insert the new node before any of the nodes in the comma separeted list
/// </summary>
/// <param name="parentNode">Parent node</param>
/// <param name="beforeNodes">comma separated list containing nodes to insert after. Left to right order</param>
/// <param name="newNode">The new node to be inserterd</param>
internal void InserAfter(XmlNode parentNode, string beforeNodes, XmlNode newNode)
{
string[] nodePaths = beforeNodes.Split(',');
foreach (string nodePath in nodePaths)
{
XmlNode node = parentNode.SelectSingleNode(nodePath, NameSpaceManager);
if (node != null)
{
parentNode.InsertAfter(newNode, node);
return;
}
}
parentNode.InsertAfter(newNode, null);
}
internal static void LoadXmlSafe(XmlDocument xmlDoc, Stream stream)
{
XmlReaderSettings settings = new XmlReaderSettings();
//Disable entity parsing (to aviod xmlbombs, External Entity Attacks etc).
#if(Core)
settings.DtdProcessing = DtdProcessing.Prohibit;
#else
settings.ProhibitDtd = true;
#endif
XmlReader reader = XmlReader.Create(stream, settings);
xmlDoc.Load(reader);
}
internal static void LoadXmlSafe(XmlDocument xmlDoc, string xml, Encoding encoding)
{
var stream = new MemoryStream(encoding.GetBytes(xml));
LoadXmlSafe(xmlDoc, stream);
}
}
}