read a particula childnode of a XML

  • Thread starter Thread starter Alain R.
  • Start date Start date
A

Alain R.

Hi,

I have the following XML file :
<?xml version='1.0' encoding='UTF-8'?>
<Database>
<ConnectionString Type="PostgreSQL v8.2.x">
<DBName>c2st</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>user</Username>
<Password>pwd</Password>
</ConnectionString>
<ConnectionString Type="MySQL v4.x">
<DBName>dbtest</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>myuser</Username>
<Password>mypwd</Password>
</ConnectionString>
</Database>

How can i retrieve only the username used for PostgreSQL connection string ?

thanks a lot,

Alain
 
Alain R. said:
Hi,

I have the following XML file :
<?xml version='1.0' encoding='UTF-8'?>
<Database>
<ConnectionString Type="PostgreSQL v8.2.x">
<DBName>c2st</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>user</Username>
<Password>pwd</Password>
</ConnectionString>
<ConnectionString Type="MySQL v4.x">
<DBName>dbtest</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>myuser</Username>
<Password>mypwd</Password>
</ConnectionString>
</Database>

How can i retrieve only the username used for PostgreSQL connection string ?

Try this (watch for line wrap):-

namespace Test
{
class Test
{

[STAThread]
public static void Main(string[] args)
{

XPathDocument doc = new XPathDocument(args[0]);
XPathNavigator nav = doc.CreateNavigator();
Console.WriteLine("Username: {0}",
nav.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/Username"));
Console.ReadKey();
}
}
}

Pass the path of your xml file as a parameter to this console app.
 
I did the following thing and it works :
XPathDocument document = new XPathDocument(strFileName);
XPathNavigator navigator = document.CreateNavigator();

XPathNavigator nd =
navigator.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/DBName");
TBDBName.Text = nd.InnerXml;

:-)

Al.

Anthony said:
Alain R. said:
Hi,

I have the following XML file :
<?xml version='1.0' encoding='UTF-8'?>
<Database>
<ConnectionString Type="PostgreSQL v8.2.x">
<DBName>c2st</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>user</Username>
<Password>pwd</Password>
</ConnectionString>
<ConnectionString Type="MySQL v4.x">
<DBName>dbtest</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>myuser</Username>
<Password>mypwd</Password>
</ConnectionString>
</Database>

How can i retrieve only the username used for PostgreSQL connection string
?

Try this (watch for line wrap):-

namespace Test
{
class Test
{

[STAThread]
public static void Main(string[] args)
{

XPathDocument doc = new XPathDocument(args[0]);
XPathNavigator nav = doc.CreateNavigator();
Console.WriteLine("Username: {0}",
nav.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/Username"));
Console.ReadKey();
}
}
}

Pass the path of your xml file as a parameter to this console app.
 
Alain R. said:
I did the following thing and it works :
XPathDocument document = new XPathDocument(strFileName);
XPathNavigator navigator = document.CreateNavigator();

XPathNavigator nd =
navigator.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/DBName");
TBDBName.Text = nd.InnerXml;

That'll probably always work for DBName but in general its not a good
approach since the text may include characters such as < and & which
InnerXml will return as &lt; and &amp; instead.

Use ToString instead that returns the equivalent of InnerText (which is what
you would use if using System.Xml).

--
Anthony Jones - MVP ASP/ASP.NET
Anthony said:
Alain R. said:
Hi,

I have the following XML file :
<?xml version='1.0' encoding='UTF-8'?>
<Database>
<ConnectionString Type="PostgreSQL v8.2.x">
<DBName>c2st</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>user</Username>
<Password>pwd</Password>
</ConnectionString>
<ConnectionString Type="MySQL v4.x">
<DBName>dbtest</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>myuser</Username>
<Password>mypwd</Password>
</ConnectionString>
</Database>

How can i retrieve only the username used for PostgreSQL connection
string
?

Try this (watch for line wrap):-

namespace Test
{
class Test
{

[STAThread]
public static void Main(string[] args)
{

XPathDocument doc = new XPathDocument(args[0]);
XPathNavigator nav = doc.CreateNavigator();
Console.WriteLine("Username: {0}",
nav.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/Username"));
Console.ReadKey();
}
}
}

Pass the path of your xml file as a parameter to this console app.
 

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

Back
Top