Reputation: 389
If suppose I have the following XML file in the following format:
<Employee>
<EmpInfo>
<Name> 1 </Name>
<Age> 23 </Age>
<Salary> 23234 </Salary>
</EmpInfo>
<EmpInfo>
<Name> 2 </Name>
<Age> 234 </Age>
<Salary> 54 </Salary>
</EmpInfo> *and so on in the similar fashion*
</Employee>
I want to display the output.
EDIT:
I tried the basic way like this:
doc = new XmlDocument();
doc.Load(@"D:\new.xml");
root = doc.DocumentElement;
txtName1.Text = root.GetElementsByTagName("name1")[0].InnerText;
textBox2.Text = root.GetElementsByTagName("empid1")[0].InnerText;
textBox3.Text = root.GetElementsByTagName("deptt1")[0].InnerText;
textBox4.Text = root.GetElementsByTagName("name2")[1].InnerText;
textBox5.Text = root.GetElementsByTagName("empid2")[1].InnerText;
textBox6.Text = root.GetElementsByTagName("deptt2")[1].InnerText;
Is there any other way apart from using GetElementsByTagName?
Upvotes: 0
Views: 224
Reputation: 1037
Your question may have a broad answer so better first you try the following
Try to Read the Xml usinng this link
Try to Create a table using this link
Upvotes: 0
Reputation: 5977
DataSet ds = new DataSet();
ds.ReadXml("xml file path");
Then you can use the dataset along with datatable and subsequent operations.e.g with repeater or datagrid or likewise
Upvotes: 0
Reputation: 2041
You can read data in DataSet
and assign that DataSet
to GridView ..
check below code
string myXMLfile = Server.MapPath("XMLFile.xml");
DataSet ds = new DataSet();
// Create new FileStream with which to read the schema.
System.IO.FileStream fsReadXml = new System.IO.FileStream(myXMLfile, System.IO.FileMode.Open);
try
{
ds.ReadXml(fsReadXml);
gridView1.DataSource = ds;
gridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
fsReadXml.Close();
}
Upvotes: 1
Reputation: 4896
I would parse the XML data to an array, then iterate through each of the EmpInfos, printing the employee name, age, and salary with a printf-like function. Perhaps print the headings as well.
Upvotes: 0
Reputation: 16698
This piece of code will give you rows in the tabular form:
XDocument doc = XDocument.Load(@"[xml file]");
var rows = doc.Descendants("EmpInfo").Select(e => new()
{
Name = e.Element("Name").Value,
Age = e.Element("Age").Value,
Salary = e.Element("Salary").Value
});
OR
class Entity
{
public string Name { get; set; }
public int Age { get; set; }
public decimal Salary { get; set; }
}
var rows = doc.Descendants("EmpInfo").Select(e => new Entity()
{
Name = e.Element("Name").Value,
Age = Convert.ToInt32(e.Element("Age").Value),
Salary = Convert.ToDecimal(e.Element("Salary").Value)
});
Upvotes: 2