locked
Saving XDocument in My.Settings/user.config RRS feed

  • Question

  • I am trying to save an XDocument into the application user settings. When I do this, nothing happens.

    First I create the XDocument.

    Dim x As XDocument = New XDocument(<test />)
    My.Settings.x = New XDocument(x)
    My.Settings.Save

    After I review the user.config file the following is generated:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <MyApp.Settings>
                <setting name="x" serializeAs="Xml">
                    <value />
                </setting>
            </MyApp.Settings>
        </userSettings>
    </configuration>

    So I know the setting is being accessed or else the setting x and the <value /> elements wouldnt exist. There is no exception thrown. So either you cannot store XML data w/in the XML Configuration file, but thought this would be serialized for storage, but maybe not, I have never attempted this. I can simply write to a file, but am not a fan of this approach, this is much cleaner and doesnt require adding more files to our application.

    Thanks,

    Steve
    Thursday, November 19, 2009 3:06 AM

Answers

  • Hi Steve,

     

    The XDocument is not serializable, and we can verify it from the .NET Reflector.  That is the reason why we cannot get the value of the XDocument typed setting is empty.  One example of a serializable is StringBuilder.   It is definition in .NET Reflector is

    ===========================================================================
    [Serializable, ComVisible(true)]

    public sealed class StringBuilder : ISerializable
    ===========================================================================

     

    After saving the StringBuilder(“Hello World”) in user.config, we get:

    ===========================================================================
              <setting name="Setting" serializeAs="Xml">

                    <value>

                        <StringBuilder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                            xmlns:xsd="http://www.w3.org/2001/XMLSchema">

                            <Capacity>16</Capacity>

                            <Length>11</Length>

                        </StringBuilder>

                    </value>

                </setting>
    ==========================================================================

     

    If you want to save the XML content in the user.config, we can set the setting type as String:

     

    Save the setting

    ==========================================================================
    My.Settings.x = doc.ToString()

    My.Settings.Save()
    ==========================================================================

     

    Load the setting back to XDocument:

    ==========================================================================
    Dim doc = XDocument.Parse(My.Settings.x)
    ==========================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, November 19, 2009 6:25 AM
    Moderator

All replies

  • Hi Steve,

     

    The XDocument is not serializable, and we can verify it from the .NET Reflector.  That is the reason why we cannot get the value of the XDocument typed setting is empty.  One example of a serializable is StringBuilder.   It is definition in .NET Reflector is

    ===========================================================================
    [Serializable, ComVisible(true)]

    public sealed class StringBuilder : ISerializable
    ===========================================================================

     

    After saving the StringBuilder(“Hello World”) in user.config, we get:

    ===========================================================================
              <setting name="Setting" serializeAs="Xml">

                    <value>

                        <StringBuilder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                            xmlns:xsd="http://www.w3.org/2001/XMLSchema">

                            <Capacity>16</Capacity>

                            <Length>11</Length>

                        </StringBuilder>

                    </value>

                </setting>
    ==========================================================================

     

    If you want to save the XML content in the user.config, we can set the setting type as String:

     

    Save the setting

    ==========================================================================
    My.Settings.x = doc.ToString()

    My.Settings.Save()
    ==========================================================================

     

    Load the setting back to XDocument:

    ==========================================================================
    Dim doc = XDocument.Parse(My.Settings.x)
    ==========================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, November 19, 2009 6:25 AM
    Moderator
  • Hi Steve,

     

    How is the problem now?  Do you need any further assistance?  If so, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by CTRLIT Tuesday, November 24, 2009 7:38 PM
    • Unmarked as answer by CTRLIT Tuesday, November 24, 2009 7:38 PM
    Monday, November 23, 2009 12:50 AM
    Moderator
  • Worked like a charm, Thanks!
    Tuesday, November 24, 2009 7:39 PM