none
Config file with empty default value (using Visual Studio 2015) RRS feed

  • Question

  • Gentlefolk,

    If I have an setting "OptionalSetting" in a C# application and the default value is an empty string the generated application config lines contains the following snippet:

                <setting name="OptionalSetting" serializeAs="String">
                    <value />
                </setting>
    


    This is an absolute pain, because if the setting needs to be set the xml needs to be edited to the form:

                <setting name="OptionalSetting" serializeAs="String">
                    <value>The set option</value>
    


    Is there any way to get Visual Studio to generate the empty default as the following (so as to minimize the editing required):

                <setting name="OptionalSetting" serializeAs="String">
                    <value></value>
    

    In this case the only thing that needs to be done is to insert the desired value. Alternatively, what tools are there for setting config file entries at install time?

    Thanks in advance.

    Andrew Ch.

    Monday, September 2, 2019 11:45 AM

All replies

  • Hi Andrew Ch. _, 

    Thank you for posting here.

    According to your description, you can refer the following code to generate config xml file.

                string path = @"xml file path";
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(path);
                XmlElement element = xdoc.CreateElement("setting");
                element.SetAttribute("name", "OptionalSetting");
                element.SetAttribute("serializeAs", "String");
                xdoc.DocumentElement.AppendChild(element);
                XmlElement element2 = xdoc.CreateElement("value");
                element2.InnerText = "";
                element.AppendChild(element2);         
                xdoc.Save(path);

    Original xml file:

    Result of the Test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 3, 2019 3:14 AM
    Moderator
  • No you cannot modify how the designer generates the file. You can put a dummy value in like a space and then it should generate it properly. However you're not supposed to be editing this stuff by hand anyway so it really shouldn't be an issue.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 3, 2019 2:30 PM
    Moderator
  • Hello Michael, thanks for your reply.

    I did try inserting a space as the default value in the Visual Studio settings. However, there is obviously a bug in Visual Studio, because doing so cause the "<value>" and "</value>" delimiters being separated by a new line (and when the application read the setting it saw it as something like "\n          ").

    As for editing the file, I am hoping that some how we can get an installer to do the edit, but at the moment I am not sure how. Maybe Xingyu's response above may be useful for that, but I am not at a point where I can try that yet.

    Tuesday, September 3, 2019 2:39 PM
  • Hi Andrew Ch. _, 

    Thanks for your feedback.

    I have a question to confirm with you.

    Do you want to set config file with C# code or Visual Studio?

    Besides,

    >> Maybe Xingyu's response above may be useful for that, but I am not at a point where I can try that yet.

    How do I need to modify my code?

    Please provide more information, and we are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 4, 2019 8:16 AM
    Moderator
  • Hello Xingyu,

    Let me try and clarify (and my apologies if I have not been clear). I am creating a C# application and using the C# project properties to add various settings, which get written into the App.config file and then end up in the ApplicationName.exe.config file when the application is built ("ApplicationName" being a placeholder for the real application name).

    There is one setting that by default I would like to be an empty string. That produces the shortened "<value />" entry in the Application.exe.config file.

    At the moment when we "install" the application, if we need the setting to be other than default the ApplicationName.exe.config file is edited by hand. Clearly this is not what we would want for the deliverable version of the product. We currently use InstallShield to produce our installers. I am trying to think ahead as to how we would change "<value />" to "<value>Some value for the setting</value>" at install time, via an installer. I am trying to work out the best way to modify the config file at install time; if I use a "search and replace" type tool then it would be easier if the default generated by Visual Studio was in the format "<value></value>". However, if I am open to better ways of doing it.

    Thanks,

    Andrew.

    Wednesday, September 4, 2019 9:12 AM
  • Hi Andrew Ch. _,

    >>I am trying to work out the best way to modify the config file at install time.

    I regret that we didn't find an effective way to modify the config file at install time.

    I note that you use InstallShield to produce installers, but InstallShield is a third-party product for which we don't provide help, so I suggest that you could post in their forums

    Thank you for your understanding.

    Best Regrads,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, September 6, 2019 1:56 AM
    Moderator