blah blah blah is here! blah blah » Close

up0down
link

I have an Xml document which contain some records like this :

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Koneksi>
<TipeKoneksi>Prepaid Connection</TipeKoneksi>
<Datasource>xe</Datasource>
<DBUser>a#</DBUser>
<DBPassword>12345678</DBPassword>
<Namakoneksi>localhost</Namakoneksi>
<Host>127.0.0.1</Host>
<Username>admin</Username>
<Password>sa</Password>
</Koneksi>
<Koneksi>
<TipeKoneksi>Standalone Connection</TipeKoneksi>
<Datasource>none</Datasource>
<DBUser>none</DBUser>
<DBPassword>none</DBPassword>
<Namakoneksi>langsung</Namakoneksi>
<Host>127.0.0.1</Host>
<Username>admin2</Username>
<Password>sa</Password>
</Koneksi>
</Root>


And the problem is How to get the element value of DBUser, Datasource, DBPassword only if i make the condition statement is if
  • Host
and
  • Username
has the same value with some certain textbox.

I've made the code something like this :

public bool OpenConnection()
{
string datasource = "";
string dbusername = "";
string dbpassword = "";
try
{
if(needdb==true){
reader = XmlReader.Create("connection.xml");
}
reader.MoveToContent();
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
if((reader.Name=="Host") && (reader.ReadElementString()==this.txtFTPAddress.Text)){
if((reader.Name=="Username") && (reader.ReadElementString()==this.txtUsername.Text)){
if (reader.Name == "Datasource") {
datasource=reader.ReadElementString();
}
if (reader.Name == "DBUser") {
dbusername=reader.ReadElementString();
}
if (reader.Name == "DBPassword") {
dbpassword=reader.ReadElementString();
}
}
}
}
}
conn = new OracleConnection("Data Source="+datasource+"; User Id="+dbusername+"; Password="+dbpassword+";");
conn.Open();
return true;
}
catch (Exception)
{
return false;
}
}


and get nothing as the result, Please help me.

last answered one year ago

1 answers

up0down
link

The problem there is that each iteration of the while loop reads the same element and not the child nodes within the same parent node. Worse still, XmlReader can only read in a forwards direction whereas you only want to use the Datasource, DBUser and DBPassword nodes if the Host and Username nodes (which always follow them in the file) have certain values.

However, it's possible to overcome these difficulties by using a switch statement and a couple of bool variables:

public bool OpenConnection()
{
string datasource = "";
string dbusername = "";
string dbpassword = "";

try
{
if (needdb)
{
reader = XmlReader.Create("connection.xml");
}
reader.MoveToContent();
bool found = false;
bool finished = false;

while (!finished && reader.Read())
{
if (reader.NodeType != XmlNodeType.Element) continue;

switch (reader.Name)
{
case "Datasource" :
datasource = reader.ReadElementString();
break;
case "DBUser" :
dbusername = reader.ReadElementString();
break;
case "DBPassword" :
dbpassword = reader.ReadElementString();
break;
case "Host" :
if (reader.ReadElementString() == this.txtFTPAddress.Text) found = true;
break;
case "Username" :
if (found && reader.ReadElementString() != this.txtUsername.Text) found = false;
if (found) finished = true;
break;
default :
break;
}

}
if (!found) return false;
conn = new OracleConnection("Data Source="+datasource+"; User Id="+dbusername+"; Password="+dbpassword+";");
conn.Open();
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (needdb) reader.Close();
}
}

Again and again...thanks a lot Mr. Vulpes it works.

Feedback