DarkSiDE
16-02-2007, 11:10
Mi serve una classe che legga un file xml e mi stampi i valori degli elementi in un form, ho preso questo esempio dall'msdn
lass Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("config.xml");
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.IsEmptyElement)
Console.WriteLine("<{0}/>", reader.Name);
else
{
Console.Write("<{0}> ", reader.Name);
reader.Read(); // Read the start tag.
if (reader.IsStartElement()) // Handle nested elements.
Console.Write("\r\n<{0}>", reader.Name);
Console.WriteLine(reader.ReadString()); //Read the text content of the element.
}
}
}
Console.ReadLine();
}
}
Il file XML è questo:
<?xml version="1.0"?>
<connessioni>
<destinazione>
<db>cet</db>
<host>127.0.0.1</host>
<user>utente postgres</user>
<password>psw postgres</password>
</destinazione>
<sorgente>
<db>cet</db>
<user>nome utente</user>
<password>psw</password>
</sorgente>
</connessioni>
Il problema è che stampa questo:
<connessioni>
<destinazione>
<host> 127.0.0.1
<user> utente postgres
<password> psw postgres
<sorgente>
<db>cet
<user> nome utente
<password> psw
come potete notare la terza riga è vuota senza stampare il corrispondente valore, come mai?!?
lass Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("config.xml");
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.IsEmptyElement)
Console.WriteLine("<{0}/>", reader.Name);
else
{
Console.Write("<{0}> ", reader.Name);
reader.Read(); // Read the start tag.
if (reader.IsStartElement()) // Handle nested elements.
Console.Write("\r\n<{0}>", reader.Name);
Console.WriteLine(reader.ReadString()); //Read the text content of the element.
}
}
}
Console.ReadLine();
}
}
Il file XML è questo:
<?xml version="1.0"?>
<connessioni>
<destinazione>
<db>cet</db>
<host>127.0.0.1</host>
<user>utente postgres</user>
<password>psw postgres</password>
</destinazione>
<sorgente>
<db>cet</db>
<user>nome utente</user>
<password>psw</password>
</sorgente>
</connessioni>
Il problema è che stampa questo:
<connessioni>
<destinazione>
<host> 127.0.0.1
<user> utente postgres
<password> psw postgres
<sorgente>
<db>cet
<user> nome utente
<password> psw
come potete notare la terza riga è vuota senza stampare il corrispondente valore, come mai?!?