By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Read XML file

P: n/a
This is my xml file content:

<?xml version="1.0"?>
<server>
<mssqlChicago>
<host>www.chicagoserver.com</host>
<user>managerA</user>
<password>ceoman</password>
<db>dbCEO</db>
</mssqlChicago>
<mssqlLA>
<host>www.LAserver.com</host>
<user>managerB</user>
<password>laman </password>
<db>dbLAWork</db>
</mssqlLA>
</server>

Any sample code to read these 2 nodes from the xml file?
Feb 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi there is a solution reading xml file to add dataset (i think u want
that)

private XmlDocument myDoc = new XmlDocument();
private DataSet myDs;

//Using XmlElement you can access
//host ,user etc. values
//just write myElemet.GetAttribute("host"); or whatever u want to take
private XmlElement myElemet;
private void LoadDataGrid()
{
myDs = new DataSet();
myDs.ReadXml("myXML.xml");
dataGrid1.DataSource = myDs.Tables[0];
}
private void LoadXML(){
myDoc.Load("your_file.xml");
}
private void Form1_Load(object sender, System.EventArgs e){
LoadDataGrid();
LoadXML();
}
Alan T yazdi:
This is my xml file content:

<?xml version="1.0"?>
<server>
<mssqlChicago>
<host>www.chicagoserver.com</host>
<user>managerA</user>
<password>ceoman</password>
<db>dbCEO</db>
</mssqlChicago>
<mssqlLA>
<host>www.LAserver.com</host>
<user>managerB</user>
<password>laman </password>
<db>dbLAWork</db>
</mssqlLA>
</server>

Any sample code to read these 2 nodes from the xml file?
Feb 7 '07 #2

P: n/a
Try something like this:

XmlDocument xdoc = new XmlDocument();
xdoc.Load(Application.StartupPath + "\\xml.xml");
XmlNodeList nodes = xdoc.SelectNodes("server");

foreach(XmlNode node in nodes)
{
//print node["mssqlChicago"].ChildNodes["host"].InnerText)
//print node["mssqlChicago"].ChildNodes["user"].InnerText)
//print
node["mssqlChicago"].ChildNodes["password"].InnerText)
//print node["mssqlChicago"].ChildNodes["db"].InnerText)
}
--

Tiago Salgado

http://weblogs.pontonetpt.com/tiagosalgado
http://www.foruns.org
http://www.portugal-a-programar.org
http://www.revista-programar.info

"Alan T" <al*************@yahoo.com.auwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
This is my xml file content:

<?xml version="1.0"?>
<server>
<mssqlChicago>
<host>www.chicagoserver.com</host>
<user>managerA</user>
<password>ceoman</password>
<db>dbCEO</db>
</mssqlChicago>
<mssqlLA>
<host>www.LAserver.com</host>
<user>managerB</user>
<password>laman </password>
<db>dbLAWork</db>
</mssqlLA>
</server>

Any sample code to read these 2 nodes from the xml file?

Feb 7 '07 #3

P: n/a
You could also get by tag name if you really wanted.

JJ
Feb 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.