1

I'm fairly new to C# and while I'm able to work on existing code and make changes some things aren't very obvious to me. I'm currently doing a Pluralsight course on C# to further my knowledge.

What I've seen is that you can create a customised class of an existing class for your own use. I saw an implementation here where an overridden property for Encoding was set. I'm working on a project where I need to create a lot of XML documents in various scenarios. I'd like to use the same settings for all and I'm hoping that it's possible to use my own class for this to avoid having to paste the same code many times over. The settings code I'd like to set when instantiating the class is below:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("\t");
settings.OmitXmlDeclaration = true;

My aim is to create a custom class that would be instantiated like below but would have the above settings already set

CustomXmlWriterSettings settings = new CustomXmlWriterSettings();
Daniel
  • 2,167
  • 5
  • 23
  • 44
  • I believe you are writing a custom XML writer and passing this CustomXML writer setting to it while Initializing right? if that is the case there is another way to do that – Ramankingdom Aug 08 '17 at 12:05

4 Answers4

3

You don't need a separate class to specify a state of an existing class. All you need is a helper method:

static class XmlHelper {
    public static XmlWriterSettings GetCustomSettings() {
        return new XmlWriterSettings {
            Indent = true,
            IndentChars = ("\t"),
            OmitXmlDeclaration = true
        };
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Daniel, with this approach from dasblinkenlight you can do this:

var configuration = XmlHelper.GetCustomSettings();

And for exmaple, retrieve indent like this:

var indent = configuration.Indent;
alaa_sayegh
  • 2,141
  • 4
  • 21
  • 37
0

This might you want

public class CustomXmlWriter : XmlWriter
        {
            public override XmlWriterSettings Settings
            {
                get
                {
                    // for this you can use method as well
                    var settings = new XmlWriterSettings();
                    settings = new XmlWriterSettings();
                    settings.Indent = true;
                    settings.IndentChars = ("\t");
                    settings.OmitXmlDeclaration = true;
                    return settings;
                }
            }

        }

And use this class anywhere

Ramankingdom
  • 1,478
  • 2
  • 12
  • 17
  • Unfortunately `XmlWriter` is a sealed type so you get errors, there's a working answer from dasblinkelight but thank you! – Daniel Aug 08 '17 at 13:07
  • @Daneil https://msdn.microsoft.com/en-us/library/system.xml.xmlwriter(v=vs.110).aspx. As per the documentarion its abstract class. XmlWriterSettings is sealed. no issues – Ramankingdom Aug 09 '17 at 03:32
-1

You might use the constructor:

public class CustomXmlWriterSettings : YourXmlWriterSettings // Use your own class as XmlWriterSettings is sealed and therefore uninheritable
{
   public CustomXmlWriterSettings()
   {
     Indent = true;
     IndentChars = ("\t");
     OmitXmlDeclaration = true;
   }

   public CustomXmlWriterSettings(bool in, string ch, bool de)
   {
     Indent = in;
     IndentChars = ch;
     OmitXmlDeclaration = de;
   }
}

You can use as many constructors as you want, as long as they all differ in parameters type and order.

  • There's a answer that was removed but it suggested the same approach and the error VS kicked out was `'CustomXmlWriterSettings': cannot derive from sealed type 'XmlWriterSettings'` – Daniel Aug 08 '17 at 11:46
  • XmlWriterSettings is a sleaed class, so it cannot be inherited – alaa_sayegh Aug 08 '17 at 11:47
  • @alaa_sayegh the other answer shows a different approach, I just need guidance on the theory of implementing it so I can understand it properly and use it. – Daniel Aug 08 '17 at 11:50