0
我已經由一個用戶更改了下面的XML文檔:更新XML節點根據屬性
<?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>
這是最初的文檔:
<?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>
一些用戶更改語言來「 DE-DE」。屬性「版本」已更新。
用戶修改後的文檔序列化爲下面的類:
<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
我現在的問題是,如果因爲SQL連接字符串改變了我的更新源XML文件,我將覆蓋的自定義設置語言。
這裏是我想達到的目標:
<?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>
這是應該的樣子:
<?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>
我已經嘗試過
有一個更新的ConnectionString新的配置文件如下: how to Update a node in xml? 這實際上工作,但我無法實現它進入我的懶類。 這也是我也發現的:How would you compare two XML Documents? 我所有這些解決方案的主要問題是,我無法在初始化期間結合序列化來管理惰性類。