Parsing XML into Array (newbie)

J

Jason Williard

I am working on a way to easily translate our website into several languages
using ASP.NET. To do so, I would like to have an XML file for each language.
The file would look like this:

<language>
<lang name="MENU_HOME">Home</lang>
<lang name="MENU_DOWNLOAD">Download</lang>
</language>

What is the easiest way to place this into an array and use it? I would like
to be able to access it by typing something like this:

lang(MENU_HOME)


I hope this makes sense. Any help would be appreciated.

Thank You,
Jason
 
G

Guest

Jason, I would:

a. Use dataset.readxml to get the data into a dataset.
b. Loop through and create a collection (e.g. a hashtable) out of it for
each language.
c. Cache the collection for each language at the application level
d. At PageInit or so point the user to the right language collection (either
he's clicked on it, something you've stored it in his cookie, etc.)
e. Get all your strings from that collection and assign them to your
controls (e.g. MyLabel.Text = MyHashTable("Menu_Home")

Instead of doing "e", there's also a nice way to make all your substitutions
en masse right before the page renders. It will save you a lot of code and
cut way down on errors. If the above is along the lines of what you want and
you get it working, let me know and I can post some code if you're interested.

hth,

Bill

P.S. You could use an array too, but some of the other collection types let
you get at "name/value" pairs faster (array access is sequential).
 
J

Jason Williard

Bill,

That is right along the lines of what I was thinking and I would deffinately
appreciate any code samples that you could offer.

Thanks,
Jason Williard
 
G

Guest

Jason, I'll post some code for the substitution at page render. A thru E are
pretty straightforward per my comments below; let me know if you have any
more specific questions.

Bill
 
J

Jason Williard

Bill,

Perhaps I'm missing something. Did you post the code in here somewhere or
are you going to post it somewhere else?

Thanks,
Jason Williard
 
G

Guest

Jason,

Here's code (vb syntax) you can use at time of page render. Credit to Hugo
(don't know his last name) for posting the brains of this out here some
months ago.

In my example, I've created my own syntax for the substitution parameters,
e.g. any output in the format [#MyStringID] will be replaced with the
corresponding value from the message table (G.Message). The format is
arbitrary, just something you shouldn't normally encounter in your html
output. Nice part about this is that no matter how you create the [#...]--by
assigning it to label.text, inserting it literally into the html, etc.--it'll
be replaced. I also plan to introduce a similar syntax (e.g. [@...]) for
allowing macro substitution of globally available variables, e.g. SessionID,
UserName, etc.

hth,

Bill

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
Dim ms As MemoryStream = New MemoryStream
Dim sw As StreamWriter = New StreamWriter(ms)
Dim htmlw As HtmlTextWriter = New HtmlTextWriter(sw)
MyBase.Render(htmlw)
htmlw.Flush()
ms.Position = 0
Dim reader As TextReader = New StreamReader(ms)
' This is where we can replace the output. DoSubParms handles the
' substitution parameters.
Response.Write(DoSubParms(reader.ReadToEnd))
End Sub

Private Const strRegexLiteral As String = "\[#\w+\]"

Public Function DoSubParms(ByVal HTMLInput As String) As String
' Go get the literals. Use regex to get keyword from [#keyword]
Dim rx As New Regex(strRegexLiteral)
Dim mc As MatchCollection = rx.Matches(HTMLInput)
Dim htmlOutput As String
htmlOutput = Regex.Replace(HTMLInput, strRegexLiteral, AddressOf
MatchReplace)
Return htmlOutput
End Function

Private Function MatchReplace(ByVal m As Match) As String
' This code is called for each match we find, so here's where we do
the
' actual substitution.
' Strip the keyword/type out of the match. The gist is that we know
we got
' here in the format [#...] or [@...], so the type is always at the
first
' position and the keyword is everything after that up to the final
' bracket.
Dim strKeyType As String = m.Value.Substring(1, 1)
Dim strKeyWord As String = m.Value.Substring(2, m.Value.Length - 3)
Dim strReturn As String = ""
Select Case strKeyType
Case "#" ' Literal
' Use keyword to get from G.Message
strReturn = G.Message(strKeyWord)
End Select
' Any errors above will result in empty string, so just return m.value
' unchanged
If strReturn = String.Empty Then
strReturn = m.Value
End If
Return strReturn
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top