Read XML file

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

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?
 
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:
 
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
 

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