Sunday, April 13, 2008
A little example of how xlinq can make life eassier on us. And the pitfalls on how to get it to work.

I have have an xml file named Translation_En.xml.

Translation_En.xml

<?xml version="1.0" encoding="utf-8" ?>
<FormType>
  <Vaccation>Vaccation</Vaccation>
  <Mission>Mission</Mission>
  <Holiday>Holiday</Holiday>
</FormType>


And then the class that will get the data and save it. Don't forget to set references to system.core, system.data.linq, system.xml and system.xml.linq.
Imports System.Linq

Public 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
And now let me explain a bit further.
First of all this class has getters and setters but that is not important ;-)

The most iportant bit is the getranslation function.

this line gets the data from the file and puts it in an Xelement object
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

The last line just takes the value of the element and puts it in a string value.
_return = _Element.Value.ToString

So 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 c

Then 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.


.Net | linq
4/13/2008 8:36 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback