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