Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used to run into all sorts of bugs making an Xml Singleton class and ended up scrapping it as I had handles all over the place. I replaced it with using two ways. One a read-only version that was for reading data, and a second Using method/statement for writing changes. </p> <p>This in general is the pattern I use:</p> <pre><code>public class Settings : IDisposable { string file = "my settings file"; XElement root; private Settings() { root = XElement.Load(file); } private void Dispose() { root.Save(file); } public static Settings Read { get { return new Settings(); } } // return read-only version public static void Write(Action&lt;Settings&gt; handler) { using(Setting settings = new Settings()) handler(settings); } // below here is implentation specific public XElement Root { get { return root; } } public string SettingA { get { return (string)(Root.Attribute("SettingA") ?? (object)string.Empty); } set { Set(Root, "SettingsA", value, true); } } // I wrote this for another StackOverflow thread /// &lt;summary&gt; /// Set any value via its .ToString() method. /// &lt;para&gt;Returns XElement of source or the new XElement if is an ELEMENT&lt;/para&gt; /// &lt;/summary&gt; /// &lt;param name="isAttribute"&gt;true for ATTRIBUTE or false for ELEMENT&lt;/param&gt; /// &lt;returns&gt;source or XElement value&lt;/returns&gt; private XElement Set(XElement source, string name, object value, bool isAttribute) { string sValue = value.ToString(); XElement eValue = source.Element(name), result = source; XAttribute aValue = source.Attribute(name); if (null != eValue) eValue.ReplaceWith(result = new XElement(name, sValue)); else if (null != aValue) aValue.ReplaceWith(new XAttribute(name, sValue)); else if (isAttribute) source.Add(new XAttribute(name, sValue)); else source.Add(result = new XElement(name, sValue)); return result; } /// &lt;summary&gt; /// Replace with for XAttribute /// &lt;/summary&gt; /// &lt;param name="source"&gt;&lt;/param&gt; /// &lt;param name="value"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static XAttribute ReplaceWith(this XAttribute source, XAttribute value) { XElement parent = source.Parent; if (null == parent) throw new Exception("Source has no parent"); source.Remove(); parent.Add(value); return value; } } </code></pre> <p>I've not used the serializer, so don't know if my pattern will fit for you. I prefer XElement.</p> <p>So to use this you'd probably write a singleton class that makes use of your non-singleton XmlSerialize class. You'd only access it through the singleton.</p> <p>But this is how I'd end up using it as is:</p> <pre><code>string settingA = Settings.Read.SettingA; </code></pre> <p>To save a value it would be:</p> <pre><code>Settings.Write(s =&gt; s.SettingA = "new value"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload