Imports System.LinqPublic Class Translation Private _Vaccation As String Private _Mission As String Private _Holiday As String Private _Language As String Public Sub New(ByVal Language As String) _language = Language End Sub Public Property Language() As String Get Return _Language End Get Set(ByVal value As String) _Language = value End Set End Property Public Property Vaccation() As String Get _Vaccation = GetTranslation("Vaccation") Return _Vaccation End Get Set(ByVal value As String) SaveTranslation("Vaccation",value) _Vaccation = value End Set End Property Public Property Mission() As String Get _Mission = GetTranslation("Mission") Return _Mission End Get Set(ByVal value As String) SaveTranslation("Mission",value) _Mission = value End Set End Property Public Property Holiday() As String Get _Holiday = GetTranslation("Holiday") Return _Holiday End Get Set(ByVal value As String) SaveTranslation("Holiday",value) _Holiday = value End Set End Property Private Function GetTranslation(ByVal Element As String) As String Dim _return As String = Element Try Dim _File As XElement = XElement.Load(GetFilename) Dim _Element As IEnumerable(Of XElement) = From c In _File.Elements(Element) Select c _return = _Element.Value.ToString Catch ex As Exception MessageBox.Show(ex.Message) End Try Return _return End Function Private Sub SaveTranslation(ByVal Element As String, ByVal value As String) Dim _File As XElement = Nothing Dim _Element As IEnumerable(Of XElement) = Nothing Try _File = XElement.Load(GetFilename) _Element = From c In _File.Elements(Element.ToString) Select c _Element(0).SetValue(value) _File.Save(GetFilename) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Function GetFilename() As String Return "Translation_En.xml" End Function End Class
Private Sub SaveTranslation(ByVal Element As String, ByVal value As String) Dim _File As XElement = Nothing Dim _Element As IEnumerable(Of XElement) = Nothing Try _File = XElement.Load(GetFilename) _Element = From c In _File.Elements(Element.ToString) Select c _Element(0).SetValue(value) _File.Save(GetFilename) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
Dim _File As XElement = XElement.Load(GetFilename)The next line takes out the Element we want. We don't need to specify the root element(a misstake I made the first time around.)Dim _Element As IEnumerable(Of XElement) = From c In _File.Elements(Element) Select c
_return = _Element.Value.ToStringSo that is how we get data out of one element.Now we want to update the file and that's what savetranslation is for, it is so easy to understand if you name your methods correctly.Again we load the file and get the correct element_File = XElement.Load(GetFilename)_Element = From c In _File.Elements(Element.ToString) Select cThen we set the first elements vale (since we only have one returned to us it has to be the first one). _Element(0).SetValue(value)And last but not least we save the file._File.Save(GetFilename)Of course you could make this a lot better. And I will in a next episode.
_File = XElement.Load(GetFilename)_Element = From c In _File.Elements(Element.ToString) Select c
_Element(0).SetValue(value)
_File.Save(GetFilename)
Powered by: newtelligence dasBlog 1.9.7174.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Christiaan Baes
E-mail