Leggere un file XML con ASP.NET

martedì 30 settembre 2003 - 12.36

Nicola Profilo | Senior Member

Ho la necessità di leggere un file XML, che contiene delle parole e inserirle in una
pagina aspx... testi, titoli intestazioni ecc. ci sono esempi?
con il web.config è semplice ma in xml??

web.config
<add key="chiave1" value="ciao"/>

pagina .aspx
<% = ConfigurationSettings.AppSettings("chiave1").ToString() %>

Nicola Profilo | Senior Member

In vb : ma il mio problema è al login appendere il mio file xml per tutta la sessione dell'utente e in funzione della sua lingua andare nelle parole o scritte
da tradurre mettere il code che mi legga dal file xml la chiave in funzione della lingua! HELP MEEE

GRAZIEEE!!!

'test lettura e selezione nodi da file xml
Dim oXml

Main

Sub Main
'creazione oggetto parser xml
set oXml = CreateObject("Microsoft.XMLDOM")
oXml.async=false

'lettura file xml
oXml.load("locale.xml")

'visualizzazione "stringa" xml
msgbox "1) Visualizzazione XML:"
msgbox (oXml.xml)

'selezione nodi italiani
msgbox "2) Nodi italiani:"
langShowNodes "IT"

'selezione nodi inglesi
msgbox "3) Nodi inglesi:"
langShowNodes "EN"

'selezione singola chiave
msgbox "4) Selezione chiave P1 K2 italiana:"
sValue = langGetKey("IT", "P1", "K2")
msgbox sValue

End Sub


Sub langShowNodes (sLang)
set oNodes = oXml.selectNodes("Items/Item[@Lang='" & sLang & "']")
msgbox "- selezionati " & oNodes.length & " nodi: "
For i = 0 To oNodes.length - 1
msgbox oNodes(i).Attributes.getNamedItem("Value").Text
Next
End Sub


Function langGetKey (sLang, sPage, sKey)
set oNode = oXml.selectSingleNode("Items/Item[@Lang='" & sLang & "'][@Page='" & sPage & "'][@Key='" & sKey & "']")
langGetKey = oNode.Attributes.getNamedItem("Value").Text
End Function

Nicola Profilo | Senior Member

File XML
P1=PAGINA 1
K1=Chiave 1

<Items>
<Item Lang="IT" Page="P1" Key="K1" Value="prima chiave italiana"/>
<Item Lang="EN" Page="P1" Key="K1" Value="first english key"/>
<Item Lang="IT" Page="P1" Key="K2" Value="seconda chiave italiana"/>
<Item Lang="EN" Page="P1" Key="K2" Value="second english key"/>
</Items>

Nicola Profilo | Senior Member

Ciao Coach,
hai tempo da scrivermi un po' in dettaglio il code...???
ti chiedo troppo? non ce ne vado fuori...

ti ringrazio in anticipo!!!!!!
i tuoi aiuti son stati preziosissimi!!!!

ciao Nicola

Nicola Profilo | Senior Member

non funziona...!!!!
che sara'???

<%@ Page Language="vb" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.xml" %>

sub page_load(sender as object, e as eventargs)

Dim Doc as new XmlDocument
Doc.load("locale.xml")

Dim list as XMLNodeList
Dim node as XMLNode
Dim attr as XmlNodeattribute
list = Doc.SelectNode("Items/Item[@Lang='" & sLang & "']")

Dim x as integer
For x =0 to list.Count -1
node = list(x)
for each attr in node.attributes
attr(x).Attributes.getNamedItem("Value").Text
'att.Attributes.getNamedItem("Value").Text
'e dai qui ti recuperi i nomi e i valori degli attributi
next
next


end sub

XML:

<Items>
<Item Lang="IT" Page="P1" Key="K1" Value="prima chiave italiana"/>
<Item Lang="EN" Page="P1" Key="K1" Value="first english key"/>
<Item Lang="IT" Page="P1" Key="K2" Value="seconda chiave italiana"/>
<Item Lang="EN" Page="P1" Key="K2" Value="second english key"/>
</Items>

Nicola Profilo | Senior Member

cIAO Coach, io ascolto sempre i tuoi consigli:
la pagina mi restituisce il codice scritto...

ti ringrazio ancora!!!!!

ciao

il mio code attualmente:

<%@ Page Language="vb" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.xml" %>

sub page_load(sender as object, e as eventargs)

Dim Doc as new XmlDocument
Doc.Load(Server.mappath("locale.xml"))

Dim list as XMLNodeList
Dim node as XMLNode
Dim attr as XmlNodeattribute
'list = Doc.SelectNode("//Item")
list = Doc.SelectNode("Items/Item[@Lang='IT']")

Dim x as integer
For x =0 to list.Count -1
node = list(x)
for each attr in node.attributes
attr.Value

next

next


end sub

'++++++++++++++++++++++++++++++++++++++++++++++++

XML:

<Items>
<Item Lang="IT" Page="P1" Key="K1" Value="prima chiave IT"/>
<Item Lang="EN" Page="P1" Key="K1" Value="first EN key"/>
<Item Lang="IT" Page="P1" Key="K2" Value="seconda chiave IT"/>
<Item Lang="EN" Page="P1" Key="K2" Value="second EN key"/>
</Items>

Nicola Profilo | Senior Member

nel body HTML che scrivo...?????

grazie Coach!!!!

ciao Nicola

Nicola Profilo | Senior Member

Ciao!
ok sono riuscito a leggere il file XML,
adesso il passo successivo è:
ho creato un vocabolario.XML diviso per pagine (le pagine .aspx del mio sito),
Page="P1" e ogni pagina a delle chiavi Key="K1" uguali per ogni singola lingua.

es:
<Items>

<!-- PAGINA 1 -->
<Item Lang="IT" Page="P1" Key="K1" Value="cane"/>
<Item Lang="EN" Page="P1" Key="K1" Value="dog"/>
<!-- PAGINA 1 -->

</Items>

Quindi voglio che nel mio testo HTML in funzione dell'utente mi venga tradotto il testo, per ora l'utente è solo ITALIANO - IT e quindi come ieri ricaviamo solo le Lang="IT", adesso in corrispondenza del testo HTML da tradurre, ad esempio (al posto della parola cane) metta del codice che mi "tiri su" da vocabolario la Key=1 e Page=1 ...

code:

sub page_load(sender as object, e as eventargs)

Dim Doc as new XmlDocument
Doc.Load(Server.mappath("vocabolario.xml"))

Dim list as XMLNodeList
Dim node as XMLNode
Dim attr as XmlNode
'TRADUZIONE ITALIANA
list = Doc.SelectNodes("Items/Item[@Lang='IT']")

Dim x as integer
For x =0 to list.Count -1
node = list(x)
for each attr in node.attributes

If attr.Name = "Value" Then
Response.write(attr.Value & "<br>")

End if

next

next


end sub


HTML:

<font face="Arial">Il mio <b>cane</b> è un lupo</font>

Al posto di cane "vorrei" mettere del codice che mi estragga dal file XML ...P1 e K1, per ora la lingua è solo IT.

HELP ME !!

GRAZIE per l'aiuto!!!

Niocla

Nicola Profilo | Senior Member

Ho utilizzato databind() creando una funzione, ma mi da erroe...

ERRORE:

Messaggio di errore del compilatore: BC30807: 'Let' and 'Set' assignment statements are no longer supported.
RIGA:
set oNode = oXml.selectSingleNode("Items/Item[@Lang='" & Slang & "'][@Page='" & Spage & "'][@Key='" & Skey & "']")

Grazie per l'aiuto!!!!

ciao Nicola

code:

<%@ Page Language="vb" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.xml" %>

<script language="VB" runat="server">

sub page_load(sender as object, e as eventargs)

Dim Doc as new XmlDocument
Doc.Load(Server.mappath("vocabolario.xml"))

Dim list as XMLNodeList
Dim node as XMLNode
Dim attr as XmlNode '.Attribute
list = Doc.SelectNodes("Items/Item[@Lang='IT']")

Dim x as integer

For x =0 to list.Count -1
node = list(x)
for each attr in node.attributes

If attr.Name = "Value" Then
Response.write(attr.Value & "<br>")

End if


next

next

DataBind()

end sub

Function LeggiXml (byval Slang as string, byval Spage as string, byval Skey as string) as string

set oNode = oXml.selectSingleNode("Items/Item[@Lang='" & Slang & "'][@Page='" & Spage & "'][@Key='" & Skey & "']")

return oNode.Attributes.getNamedItem("Value").Text

End Function

</script>



<HTML>
<HEAD><TITLE></TITLE>


</HEAD>

<BODY vLink=#ffffff aLink=#ffffff link=#ffffff leftMargin=0 topMargin=0>

<font face="Arial">Il mio <%# LeggiXml("IT", "P1", "K1") %> è un lupo</font>

</BODY>

</HTML>

Nicola Profilo | Senior Member

E DOVE Coach??

sono quasi disperato...
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5