@@ -17,7 +17,7 @@ namespace XmlAbstraction
1717 // Only the Save() method should do direct edits to the XDocument object of the class named "Doc".
1818 // The rest should just use the dictionaries for the changes to be applied to the xml in the Save()
1919 // 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
2121 {
2222 /// <summary>
2323 /// Initializes a new instance of the <see cref="XmlObject"/> class
@@ -113,11 +113,6 @@ public XmlObject(string xmlfilename, string fallbackxmlcontent, bool saveToCurre
113113 this . Doc = ( fileSize > 0 ) ? XDocument . Load ( xmlfilename ) : XDocument . Parse ( fallbackxmlcontent ) ;
114114 }
115115
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-
121116 private object ObjLock { get ; set ; }
122117
123118 private XDocument Doc { get ; set ; }
@@ -181,15 +176,9 @@ private bool HasChangedExternally
181176 /// but only if it has changed. If the file was not saved it
182177 /// will be saved first.
183178 /// </summary>
184- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
185179 /// <exception cref="InvalidOperationException">Cannot reopen on read-only instances.</exception>
186180 public void ReopenFile ( )
187181 {
188- if ( this . IsDisposed )
189- {
190- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
191- }
192-
193182 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
194183 {
195184 this . Save ( ) ;
@@ -202,27 +191,21 @@ public void ReopenFile()
202191 }
203192
204193 /// <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"/> .
206195 ///
207196 /// This method can also remove the attribute by setting the value to null.
208197 ///
209198 /// If Element does not exist yet it will be created automatically with an
210199 /// empty value as well as making the attribute as if the Element was
211200 /// pre-added before calling this function.
212201 /// </summary>
213- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
214202 /// <exception cref="Exception">Attribute already exists in the xml file.</exception>
215203 /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
216204 /// <param name="elementname">The name of the element to add a attribute to.</param>
217205 /// <param name="attributename">The name of the attribute to add.</param>
218206 /// <param name="attributevalue">The value of the attribute.</param>
219207 public void AddAttribute ( string elementname , string attributename , object attributevalue )
220208 {
221- if ( this . IsDisposed )
222- {
223- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
224- }
225-
226209 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
227210 {
228211 var elem = this . Doc . Root . Element ( elementname ) ;
@@ -331,17 +314,11 @@ public void AddAttribute(string elementname, string attributename, object attrib
331314 ///
332315 /// If Element does not exist yet it will be created automatically.
333316 /// </summary>
334- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
335317 /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
336318 /// <param name="elementname">The name of the element to write to or create.</param>
337319 /// <param name="value">The value for the element.</param>
338320 public void Write ( string elementname , string value )
339321 {
340- if ( this . IsDisposed )
341- {
342- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
343- }
344-
345322 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
346323 {
347324 var elem = this . Doc . Root . Element ( elementname ) ;
@@ -396,7 +373,6 @@ public void Write(string elementname, string value)
396373 ///
397374 /// If Element does not exist yet it will be created automatically.
398375 /// </summary>
399- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
400376 /// <exception cref="Exception">Attribute already exists in the xml file.</exception>
401377 /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
402378 /// <param name="elementname">
@@ -407,11 +383,6 @@ public void Write(string elementname, string value)
407383 /// <param name="attributevalue">The value of the attribute to use.</param>
408384 public void Write ( string elementname , string attributename , string attributevalue )
409385 {
410- if ( this . IsDisposed )
411- {
412- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
413- }
414-
415386 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
416387 {
417388 this . AddAttribute ( elementname , attributename , attributevalue ) ;
@@ -428,18 +399,12 @@ public void Write(string elementname, string attributename, string attributevalu
428399 ///
429400 /// If Elements do not exist yet they will be created automatically.
430401 /// </summary>
431- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
432402 /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
433403 /// <param name="parentelementname">parrent element name of the subelement.</param>
434404 /// <param name="elementname">The name to use when writing subelement(s).</param>
435405 /// <param name="values">The array of values to use for the subelement(s).</param>
436406 public void Write ( string parentelementname , string elementname , string [ ] values )
437407 {
438- if ( this . IsDisposed )
439- {
440- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
441- }
442-
443408 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
444409 {
445410 var elem = this . Doc . Root . Element ( parentelementname ) ;
@@ -490,17 +455,11 @@ public void Write(string parentelementname, string elementname, string[] values)
490455 ///
491456 /// If Element does not exist yet it will be created automatically with an empty value.
492457 /// </summary>
493- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
494458 /// <exception cref="InvalidOperationException">When the Element does not exist in a read-only instance.</exception>
495459 /// <param name="elementname">The element name to read the value from.</param>
496460 /// <returns>The value of the input element or <see cref="string.Empty"/>.</returns>
497461 public string Read ( string elementname )
498462 {
499- if ( this . IsDisposed )
500- {
501- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
502- }
503-
504463 var elem = this . Doc . Root . Element ( elementname ) ;
505464 if ( elem != null
506465 || this . ElementsAdded . ContainsKey ( elementname )
@@ -528,18 +487,12 @@ public string Read(string elementname)
528487 /// If Element and the attribute does not exist yet it will be created automatically
529488 /// with an empty value.
530489 /// </summary>
531- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
532490 /// <exception cref="InvalidOperationException">When the Element does not exist in a read-only instance.</exception>
533491 /// <param name="elementname">The element name to get the value of a attribute.</param>
534492 /// <param name="attributename">The name of the attribute to get the value of.</param>
535493 /// <returns>The value of the input element or <see cref="string.Empty"/>.</returns>
536494 public string Read ( string elementname , string attributename )
537495 {
538- if ( this . IsDisposed )
539- {
540- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
541- }
542-
543496 var elem = this . Doc . Root . Element ( elementname ) ;
544497 if ( elem == null )
545498 {
@@ -583,7 +536,6 @@ public string Read(string elementname, string attributename)
583536 /// If Parent Element does not exist yet it will be created automatically
584537 /// with an empty value. In that case an empty string array is returned.
585538 /// </summary>
586- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
587539 /// <exception cref="InvalidOperationException">When the Element does not exist in a read-only instance.</exception>
588540 /// <param name="parentelementname">The name of the parrent element of the subelement(s).</param>
589541 /// <param name="elementname">The name of the subelements to get their values.</param>
@@ -596,11 +548,6 @@ public string Read(string elementname, string attributename)
596548 /// </returns>
597549 public string [ ] Read ( string parentelementname , string elementname , object unused = null )
598550 {
599- if ( this . IsDisposed )
600- {
601- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
602- }
603-
604551 var elem = this . Doc . Descendants ( parentelementname ) ;
605552 var strarray = new string [ ] { } ;
606553 foreach ( var element in elem )
@@ -627,17 +574,11 @@ public string[] Read(string parentelementname, string elementname, object unused
627574 /// Deletes an xml element using the element name.
628575 /// Can also delete not only the parrent element but also subelements with it.
629576 /// </summary>
630- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
631577 /// <exception cref="ArgumentException">elementname does not exist in the xml or in pending edits.</exception>
632578 /// <exception cref="InvalidOperationException">When the object is a read-only instance.</exception>
633579 /// <param name="elementname">The element name of the element to delete.</param>
634580 public void Delete ( string elementname )
635581 {
636- if ( this . IsDisposed )
637- {
638- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
639- }
640-
641582 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
642583 {
643584 var elem = this . Doc . Root . Element ( elementname ) ;
@@ -667,18 +608,12 @@ public void Delete(string elementname)
667608 /// <summary>
668609 /// Removes an xml attribute using the element name and the name of the attribute.
669610 /// </summary>
670- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
671611 /// <exception cref="ArgumentException">elementname or attributename does not exist in the xml or in pending edits.</exception>
672612 /// <exception cref="InvalidOperationException">When the object is a read-only instance.</exception>
673613 /// <param name="elementname">The element name that has the attribute to delete.</param>
674614 /// <param name="attributename">The name of the attribute to delete.</param>
675615 public void Delete ( string elementname , string attributename )
676616 {
677- if ( this . IsDisposed )
678- {
679- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
680- }
681-
682617 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
683618 {
684619 var elem = this . Doc . Root . Element ( elementname ) ;
@@ -731,19 +666,13 @@ public void Delete(string elementname, string attributename)
731666 /// <summary>
732667 /// Saves the underlying XML file if it changed.
733668 /// </summary>
734- /// <exception cref="ObjectDisposedException"><see cref="XmlObject"/> is disposed.</exception>
735669 public void Save ( )
736670 {
737671 // do not save in memory xml. It should be read only.
738672 if ( ! this . CachedXmlfilename . Equals ( ":memory" ) )
739673 {
740674 lock ( this . ObjLock )
741675 {
742- if ( this . IsDisposed )
743- {
744- throw new ObjectDisposedException ( nameof ( XmlObject ) ) ;
745- }
746-
747676 if ( this . HasChangedExternally && this . Exists )
748677 {
749678 // reopen file to apply changes at runtime to it.
@@ -831,11 +760,6 @@ public void Save()
831760 }
832761 }
833762
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-
839763 // Summary:
840764 // Adds an Element to the XmlObject but verifies it does not exist first.
841765 //
@@ -898,25 +822,6 @@ private void SaveAddedSubelements(XElement xElement, XmlElementData elemdata)
898822 }
899823 }
900824
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-
920825 private class XmlAttributeData
921826 {
922827 internal string AttributeName { get ; set ; } = string . Empty ;
0 commit comments