XML : Update XML Nodes depending on Attribute

I have the following XML Document that has been changed by a user:

  <?xml version="1.0" encoding="utf-8" ?>  <Cfg xmlns="AddIn" version="161012">    <SQLConnectionString version="161012">SomeConnectionString</SQLConnectionString>    <Locale version="161012">      <Language version="161013">de-DE</Language>      <LocalSetting version="161012">en-US</LocalSetting>    </Locale>  </Cfg>    

This is the initial Document:

  <?xml version="1.0" encoding="utf-8" ?>  <Cfg xmlns="AddIn" version="161012">    <SQLConnectionString version="161012">SomeConnectionString</SQLConnectionString>    <Locale version="161012">      <Language version="161012">en-US</Language>      <LocalSetting version="161012">en-US</LocalSetting>    </Locale>  </Cfg>    

Some user changed the Language to "de-DE". The Attribute "version" has been updated.

The user modified Document is Serialized into the following class:

  <Serializable()>  Public Class Cfg        Private Shared CONFIG_LOCATION As String = GetFolderPath(SpecialFolder.ApplicationData) & "\MyProgram\"      Private Shared CONFIG_FNAME As String = "Cfg.xml"      Private Shared CONFIG_FULLPATH As String = CONFIG_LOCATION & CONFIG_FNAME      Private Shared CONFIG_ASSEMBLY_PATH As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) & "\cfg\"  #Region "Singleton"      Private Shared ReadOnly _instance As New System.Lazy(Of Cfg)(Function()                                                                            'Write and read                                                                            Dim _Cfg As New Cfg                                                                            If Not File.Exists(CONFIG_FULLPATH) Then                                                                                'copy xml config file                                                                                If Not Directory.Exists(CONFIG_LOCATION) Then                                                                                    Directory.CreateDirectory(CONFIG_LOCATION)                                                                                End If                                                                                  File.Copy(CONFIG_ASSEMBLY_PATH & CONFIG_FNAME, CONFIG_FULLPATH)                                                                            Else                                                                                'This is the point where I need to apply the updates to the xml document                                                                              End If                                                                              Dim helper = New XmlSerializerHelper(Of Cfg)()                                                                            _Cfg = helper.Read(CONFIG_FULLPATH)                                                                              Return _Cfg                                                                          End Function, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)      Public Shared ReadOnly Property Instance() As Cfg          Get              Return _instance.Value          End Get      End Property  #End Region        Private _Locale As Locale      Private _SQLConnectionString As String      Public Property Locale() As Locale          Get              Return _Locale          End Get          Set(value As Locale)              _Locale = value          End Set      End Property        Public Property SQLConnectionString As String          Get              Return _SQLConnectionString          End Get          Set(value As String)              _SQLConnectionString = value          End Set      End Property        Private Sub New()          End Sub          Public Function SaveConfigData() As Boolean          Dim helper = New XmlSerializerHelper(Of Cfg)()          Dim obj = Me          helper.Save(CONFIG_FNAME, obj)          Return True      End Function    End Class    <Serializable()>  Public Class Locale      Private _Language As String      Public Property Language As String          Get              Return _Language          End Get          Set(value As String)              _Language = value          End Set        End Property        Private _LocalSetting As String      Public Property LocalSetting As String          Get              Return _LocalSetting          End Get          Set(value As String)              _LocalSetting = value          End Set      End Property        Public Sub New()      End Sub  End Class    

My Problem now is, if I update the Source XML file because the SQL Connection string has changed, I would overwrite the custom setting of the language.

Here is what I want to achieve:

New config file that has an updated ConnectionString:

  <?xml version="1.0" encoding="utf-8" ?>  <Cfg xmlns="AddIn" version="161012">    <SQLConnectionString version="161013">ThisIsTheNewConnectionString</SQLConnectionString>    <Locale version="161012">      <Language version="161012">en-US</Language>      <LocalSetting version="161012">en-US</LocalSetting>    </Locale>  </Cfg>    

This is how it should look like:

  <?xml version="1.0" encoding="utf-8" ?>  <Cfg xmlns="AddIn" version="161012">    <SQLConnectionString version="161013">ThisIsTheNewConnectionString</SQLConnectionString>    <Locale version="161012">      <Language version="161013">de-DE</Language>      <LocalSetting version="161012">en-US</LocalSetting>    </Locale>  </Cfg>    

I already tried the following: how to Update a node in xml? This actually worked, but i was not able to implement it into my lazy class. This is what I also have found: How would you compare two XML Documents? My main problem for all those solutions was, that I was not able to manage the lazy class in combination with serialization during initialization.

1 comment:

  1. Are you looking to make money from your visitors by using popunder ads?
    In case you are, have you ever used PropellerAds?

    ReplyDelete