Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit 6f9a540

Browse files
committed
Remove IDisposable from XmlObject.
1 parent 102c96d commit 6f9a540

File tree

4 files changed

+5
-143
lines changed

4 files changed

+5
-143
lines changed

XmlAbstraction.Test/XmlObjectUnitTest.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,50 +31,14 @@ public void TestClassReopenFile()
3131
$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml");
3232
fstrm.Write(Encoding.UTF8.GetBytes(testXml), 0, testXml.Length);
3333
fstrm.Dispose();
34-
xmlObj.Dispose();
3534
xmlObj = new XmlObject(
3635
$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml",
3736
testXml);
3837
NoThrows(() => xmlObj.ReopenFile());
39-
xmlObj.Dispose();
4038
File.Delete(
4139
$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml");
4240
}
4341

44-
[Fact]
45-
public void TestClassDoubleDispose()
46-
{
47-
var testXml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
48-
<test>
49-
</test>";
50-
var xmlObj = new XmlObject(testXml);
51-
xmlObj.Dispose();
52-
xmlObj.Dispose();
53-
}
54-
55-
[Fact]
56-
public void TestClassDisposedExceptions()
57-
{
58-
var testXml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
59-
<test>
60-
</test>";
61-
var xmlObj = new XmlObject(testXml);
62-
xmlObj.Dispose();
63-
64-
// test to make sure that ObjectDisposedException is thrown.
65-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.AddAttribute("test4", "test", "test"));
66-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Write("test", "test"));
67-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Write("test2", "test", "test"));
68-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Write("test3", "test31", new string[] { "test1", "test2", "test3" }));
69-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Read("test"));
70-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Read("test2", "test"));
71-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Read("test3", "test31", null));
72-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Delete("test"));
73-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Delete("test2", "test"));
74-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.ReopenFile());
75-
Assert.ThrowsAny<ObjectDisposedException>(() => xmlObj.Save());
76-
}
77-
7842
[Fact]
7943
public void TestClassEdits()
8044
{
@@ -96,15 +60,12 @@ public void TestClassEdits()
9660
Assert.ThrowsAny<InvalidOperationException>(() => xmlObj.Delete("test"));
9761
Assert.ThrowsAny<InvalidOperationException>(() => xmlObj.Delete("test2", "test"));
9862
Assert.ThrowsAny<InvalidOperationException>(() => xmlObj.ReopenFile());
99-
xmlObj.Dispose();
10063
xmlObj = new XmlObject(testXmlNoRoot);
10164
// reopen data from a file.
102-
xmlObj.Dispose();
10365
var fstrm = File.Create(
10466
$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml");
10567
fstrm.Write(Encoding.UTF8.GetBytes(testXml), 0, testXml.Length);
10668
fstrm.Dispose();
107-
xmlObj.Dispose();
10869
xmlObj = new XmlObject(
10970
$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml",
11071
testXml);
@@ -124,15 +85,11 @@ public void TestClassEdits()
12485
NoThrows(() => xmlObj.Delete("test"));
12586
Assert.ThrowsAny<ArgumentException>(() => xmlObj.Delete("test2", "test"));
12687
NoThrows(() => xmlObj.Save());
127-
xmlObj.Dispose();
12888
File.Delete(
12989
$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml");
13090
xmlObj = new XmlObject($"{Path.DirectorySeparatorChar}test.xml", testXml, true);
131-
xmlObj.Dispose();
13291
xmlObj = new XmlObject($"{Path.DirectorySeparatorChar}test.xml", testXml, true);
133-
xmlObj.Dispose();
13492
xmlObj = new XmlObject($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml", testXml, true);
135-
xmlObj.Dispose();
13693
}
13794
}
13895
}

XmlAbstraction/XmlAbstraction.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<Authors>AraHaan</Authors>
1010
<Company />
11-
<Version>1.0.1</Version>
11+
<Version>1.1.0</Version>
1212
<Description>A library that contains a System.Xml and System.Xml.Linq abstraction class.</Description>
1313
<Copyright>Copyright 2018</Copyright>
1414
<PackageTags>XML</PackageTags>
1515
<RepositoryType>git</RepositoryType>
1616
<RepositoryUrl>https://github.com/AraHaan/XmlAbstraction/</RepositoryUrl>
1717
<PackageProjectUrl>https://github.com/AraHaan/XmlAbstraction/</PackageProjectUrl>
1818
<PackageLicenseUrl>https://github.com/AraHaan/XmlAbstraction/blob/master/LICENSE</PackageLicenseUrl>
19-
<PackageReleaseNotes>A bit of changes and implemented TODOs and some fixes as well.</PackageReleaseNotes>
19+
<PackageReleaseNotes>A breaking change removing IDisposable from XmlObject.</PackageReleaseNotes>
2020
<PublishDocumentationFile>true</PublishDocumentationFile>
2121
</PropertyGroup>
2222

XmlAbstraction/XmlObject.cs

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.0.1.0-alpha{build}
1+
version: 1.1.0.0-alpha{build}
22
branches:
33
only:
44
- master

0 commit comments

Comments
 (0)