@@ -17,7 +17,7 @@ namespace XmlAbstraction
17
17
// Only the Save() method should do direct edits to the XDocument object of the class named "Doc".
18
18
// The rest should just use the dictionaries for the changes to be applied to the xml in the Save()
19
19
// method if the xml is not read-only. I did this to support read only memory access of xml.
20
- public class XmlObject : IDisposable
20
+ public class XmlObject
21
21
{
22
22
/// <summary>
23
23
/// Initializes a new instance of the <see cref="XmlObject"/> class
@@ -113,11 +113,6 @@ public XmlObject(string xmlfilename, string fallbackxmlcontent, bool saveToCurre
113
113
this . Doc = ( fileSize > 0 ) ? XDocument . Load ( xmlfilename ) : XDocument . Parse ( fallbackxmlcontent ) ;
114
114
}
115
115
116
- /// <summary>
117
- /// Gets a value indicating whether the <see cref="XmlObject"/> is disposed.
118
- /// </summary>
119
- public bool IsDisposed { get ; private set ; } = false ;
120
-
121
116
private object ObjLock { get ; set ; }
122
117
123
118
private XDocument Doc { get ; set ; }
@@ -181,15 +176,9 @@ private bool HasChangedExternally
181
176
/// but only if it has changed. If the file was not saved it
182
177
/// will be saved first.
183
178
/// </summary>
184
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
185
179
/// <exception cref="InvalidOperationException">Cannot reopen on read-only instances.</exception>
186
180
public void ReopenFile ( )
187
181
{
188
- if ( this . IsDisposed )
189
- {
190
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
191
- }
192
-
193
182
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
194
183
{
195
184
this . Save ( ) ;
@@ -202,27 +191,21 @@ public void ReopenFile()
202
191
}
203
192
204
193
/// <summary>
205
- /// Adds or edits an attribute in an Element and sets it's value in the XMLObject .
194
+ /// Adds or edits an attribute in an Element and sets it's value in the <see cref="XmlObject"/> .
206
195
///
207
196
/// This method can also remove the attribute by setting the value to null.
208
197
///
209
198
/// If Element does not exist yet it will be created automatically with an
210
199
/// empty value as well as making the attribute as if the Element was
211
200
/// pre-added before calling this function.
212
201
/// </summary>
213
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
214
202
/// <exception cref="Exception">Attribute already exists in the xml file.</exception>
215
203
/// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
216
204
/// <param name="elementname">The name of the element to add a attribute to.</param>
217
205
/// <param name="attributename">The name of the attribute to add.</param>
218
206
/// <param name="attributevalue">The value of the attribute.</param>
219
207
public void AddAttribute ( string elementname , string attributename , object attributevalue )
220
208
{
221
- if ( this . IsDisposed )
222
- {
223
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
224
- }
225
-
226
209
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
227
210
{
228
211
var elem = this . Doc . Root . Element ( elementname ) ;
@@ -331,17 +314,11 @@ public void AddAttribute(string elementname, string attributename, object attrib
331
314
///
332
315
/// If Element does not exist yet it will be created automatically.
333
316
/// </summary>
334
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
335
317
/// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
336
318
/// <param name="elementname">The name of the element to write to or create.</param>
337
319
/// <param name="value">The value for the element.</param>
338
320
public void Write ( string elementname , string value )
339
321
{
340
- if ( this . IsDisposed )
341
- {
342
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
343
- }
344
-
345
322
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
346
323
{
347
324
var elem = this . Doc . Root . Element ( elementname ) ;
@@ -396,7 +373,6 @@ public void Write(string elementname, string value)
396
373
///
397
374
/// If Element does not exist yet it will be created automatically.
398
375
/// </summary>
399
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
400
376
/// <exception cref="Exception">Attribute already exists in the xml file.</exception>
401
377
/// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
402
378
/// <param name="elementname">
@@ -407,11 +383,6 @@ public void Write(string elementname, string value)
407
383
/// <param name="attributevalue">The value of the attribute to use.</param>
408
384
public void Write ( string elementname , string attributename , string attributevalue )
409
385
{
410
- if ( this . IsDisposed )
411
- {
412
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
413
- }
414
-
415
386
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
416
387
{
417
388
this . AddAttribute ( elementname , attributename , attributevalue ) ;
@@ -428,18 +399,12 @@ public void Write(string elementname, string attributename, string attributevalu
428
399
///
429
400
/// If Elements do not exist yet they will be created automatically.
430
401
/// </summary>
431
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
432
402
/// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
433
403
/// <param name="parentelementname">parrent element name of the subelement.</param>
434
404
/// <param name="elementname">The name to use when writing subelement(s).</param>
435
405
/// <param name="values">The array of values to use for the subelement(s).</param>
436
406
public void Write ( string parentelementname , string elementname , string [ ] values )
437
407
{
438
- if ( this . IsDisposed )
439
- {
440
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
441
- }
442
-
443
408
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
444
409
{
445
410
var elem = this . Doc . Root . Element ( parentelementname ) ;
@@ -490,17 +455,11 @@ public void Write(string parentelementname, string elementname, string[] values)
490
455
///
491
456
/// If Element does not exist yet it will be created automatically with an empty value.
492
457
/// </summary>
493
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
494
458
/// <exception cref="InvalidOperationException">When the Element does not exist in a read-only instance.</exception>
495
459
/// <param name="elementname">The element name to read the value from.</param>
496
460
/// <returns>The value of the input element or <see cref="string.Empty"/>.</returns>
497
461
public string Read ( string elementname )
498
462
{
499
- if ( this . IsDisposed )
500
- {
501
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
502
- }
503
-
504
463
var elem = this . Doc . Root . Element ( elementname ) ;
505
464
if ( elem != null
506
465
|| this . ElementsAdded . ContainsKey ( elementname )
@@ -528,18 +487,12 @@ public string Read(string elementname)
528
487
/// If Element and the attribute does not exist yet it will be created automatically
529
488
/// with an empty value.
530
489
/// </summary>
531
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
532
490
/// <exception cref="InvalidOperationException">When the Element does not exist in a read-only instance.</exception>
533
491
/// <param name="elementname">The element name to get the value of a attribute.</param>
534
492
/// <param name="attributename">The name of the attribute to get the value of.</param>
535
493
/// <returns>The value of the input element or <see cref="string.Empty"/>.</returns>
536
494
public string Read ( string elementname , string attributename )
537
495
{
538
- if ( this . IsDisposed )
539
- {
540
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
541
- }
542
-
543
496
var elem = this . Doc . Root . Element ( elementname ) ;
544
497
if ( elem == null )
545
498
{
@@ -583,7 +536,6 @@ public string Read(string elementname, string attributename)
583
536
/// If Parent Element does not exist yet it will be created automatically
584
537
/// with an empty value. In that case an empty string array is returned.
585
538
/// </summary>
586
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
587
539
/// <exception cref="InvalidOperationException">When the Element does not exist in a read-only instance.</exception>
588
540
/// <param name="parentelementname">The name of the parrent element of the subelement(s).</param>
589
541
/// <param name="elementname">The name of the subelements to get their values.</param>
@@ -596,11 +548,6 @@ public string Read(string elementname, string attributename)
596
548
/// </returns>
597
549
public string [ ] Read ( string parentelementname , string elementname , object unused = null )
598
550
{
599
- if ( this . IsDisposed )
600
- {
601
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
602
- }
603
-
604
551
var elem = this . Doc . Descendants ( parentelementname ) ;
605
552
var strarray = new string [ ] { } ;
606
553
foreach ( var element in elem )
@@ -627,17 +574,11 @@ public string[] Read(string parentelementname, string elementname, object unused
627
574
/// Deletes an xml element using the element name.
628
575
/// Can also delete not only the parrent element but also subelements with it.
629
576
/// </summary>
630
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
631
577
/// <exception cref="ArgumentException">elementname does not exist in the xml or in pending edits.</exception>
632
578
/// <exception cref="InvalidOperationException">When the object is a read-only instance.</exception>
633
579
/// <param name="elementname">The element name of the element to delete.</param>
634
580
public void Delete ( string elementname )
635
581
{
636
- if ( this . IsDisposed )
637
- {
638
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
639
- }
640
-
641
582
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
642
583
{
643
584
var elem = this . Doc . Root . Element ( elementname ) ;
@@ -667,18 +608,12 @@ public void Delete(string elementname)
667
608
/// <summary>
668
609
/// Removes an xml attribute using the element name and the name of the attribute.
669
610
/// </summary>
670
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
671
611
/// <exception cref="ArgumentException">elementname or attributename does not exist in the xml or in pending edits.</exception>
672
612
/// <exception cref="InvalidOperationException">When the object is a read-only instance.</exception>
673
613
/// <param name="elementname">The element name that has the attribute to delete.</param>
674
614
/// <param name="attributename">The name of the attribute to delete.</param>
675
615
public void Delete ( string elementname , string attributename )
676
616
{
677
- if ( this . IsDisposed )
678
- {
679
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
680
- }
681
-
682
617
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
683
618
{
684
619
var elem = this . Doc . Root . Element ( elementname ) ;
@@ -731,19 +666,13 @@ public void Delete(string elementname, string attributename)
731
666
/// <summary>
732
667
/// Saves the underlying XML file if it changed.
733
668
/// </summary>
734
- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
735
669
public void Save ( )
736
670
{
737
671
// do not save in memory xml. It should be read only.
738
672
if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
739
673
{
740
674
lock ( this . ObjLock )
741
675
{
742
- if ( this . IsDisposed )
743
- {
744
- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
745
- }
746
-
747
676
if ( this . HasChangedExternally && this . Exists )
748
677
{
749
678
// reopen file to apply changes at runtime to it.
@@ -831,11 +760,6 @@ public void Save()
831
760
}
832
761
}
833
762
834
- /// <summary>
835
- /// Disposes of the <see cref="XmlObject"/> and saves the underlying XML file if it changed.
836
- /// </summary>
837
- public void Dispose ( ) => this . Dispose ( true ) ;
838
-
839
763
// Summary:
840
764
// Adds an Element to the XmlObject but verifies it does not exist first.
841
765
//
@@ -898,25 +822,6 @@ private void SaveAddedSubelements(XElement xElement, XmlElementData elemdata)
898
822
}
899
823
}
900
824
901
- private void Dispose ( bool disposing )
902
- {
903
- if ( ! this . IsDisposed )
904
- {
905
- this . Save ( ) ;
906
- this . Exists = false ;
907
- this . HasChanged = false ;
908
-
909
- // remove everything from the Lists/Dictonaries then destroy them.
910
- this . ElementsAdded = null ;
911
- this . ElementsEdits = null ;
912
- this . ElementAttributesDeleted = null ;
913
- this . ElementsDeleted = null ;
914
- this . Doc = null ;
915
- this . CachedXmlfilename = string . Empty ;
916
- this . IsDisposed = true ;
917
- }
918
- }
919
-
920
825
private class XmlAttributeData
921
826
{
922
827
internal string AttributeName { get ; set ; } = string . Empty ;
0 commit comments