How to sort Name by asc from XML file and populate the dropdown?



Hi
This is the frequently asked question in interview. “Write a program to sort the Empname by asc from Xml file and populate the dropdown”.

We can do using 2 approach
1. using dataset and dataview concept
2. using Linq to Xml concept

Here i m writting code using dataset and dataview concept
Step1: Create some Xml file like this

<?xml version="1.0" encoding="utf-8" ?>
<Emps>
<Emp>
<Id>1</Id>
<Name>Ram</Name>
<Address>xBangalore</Address>
</Emp>
<Emp>
<Id>2</Id>
<Name>RamMohan</Name>
<Address>Chenai</Address>
</Emp>
<Emp>
<Id>3</Id>
<Name>Ram</Name>
<Address>Nepal</Address>
</Emp>
</Emps>

Step 2: Write the C# code in codebehind file like this

protected void Page_Load(object sender, EventArgs e)
{

string filepath = Server.MapPath("../Emp.xml");
using (DataSet ds = new DataSet())
{
ds.ReadXml(filepath);
DataView dv = new DataView();
dv =ds.Tables["Emp"].DefaultView;
dv.RowFilter = "Name='Ram'";
dv.Sort = "Address Asc";
DropDownList1.DataSource = dv;
DropDownList1.DataTextField = "Address";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();

}

}

I hope it will help to someone.

Advertisements

How to insert/display image from XML file in asp.net?


We can do this task like this

Step1: create the Xml file in asp.net project like this

<?xml version=”1.0″ encoding=”utf-8″?>

<ImgTest>

<EmpDetails>

<EmpName> </EmpName>

<EmpSal> </EmpSal>

<ImgUrl> </ImgUrl>

</EmpDetails>

</ImgTest>

Step2: Create one Image Folder for storing the Image i.e UploadImages

Step3: Create the corresponding design page in asp.net and take one grid view to display the Emp detail with image. Configure gridview like this

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="EmpName" DataField="EmpName" />
<asp:BoundField HeaderText="EmpSal" DataField="EmpSal" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>

<asp:Image ID="Img1" runat="server" ImageUrl='<%# String.Format("~\\UploadImages\\{0}",Eval("ImgUrl"))%>’ Height="100px" Width="100px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Note: here I m storing imgae in “UploadImages” folder and ImageName in database
Step4:On Button click, write the code in code behind like this

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

public partial class XmlImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

//This code is for insertion in XML file
protected void BtnSubmit_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (CheckFileType(FileUpload1.FileName))
{
String filePath = “~/UploadImages/” + FileUpload1.FileName;
FileUpload1.SaveAs(MapPath(filePath));
}
}

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath(@”App_Data\ImgTest.xml”));
XmlElement newEmpDetails= xmldoc.CreateElement(“EmpDetails”);
XmlElement XmlEmpName= xmldoc.CreateElement(“EmpName”);
XmlElement XmlEmpSal = xmldoc.CreateElement(“EmpSal”);
XmlElement XmlImgUrl = xmldoc.CreateElement(“ImgUrl”);

XmlEmpName.InnerText = this.TextBox1.Text.Trim();
XmlEmpSal.InnerText = this.TextBox2.Text.Trim();
string vbfile = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName.ToString());
XmlImgUrl.InnerText = FileUpload1.FileName.ToString();

newEmpDetails.AppendChild(XmlEmpName);
newEmpDetails.AppendChild(XmlEmpSal);
newEmpDetails.AppendChild(XmlImgUrl);
xmldoc.DocumentElement.AppendChild(newEmpDetails);

xmldoc.Save(Server.MapPath(@”App_Data\ImgTest.xml”));

TextBox1.Text = “”;
TextBox2.Text = “”;
fillGrid();

}

//This code is filling the Gridview
protected void fillGrid()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(@”App_Data\ImgTest.xml”));
if (ds.Tables.Count > 0)
{

GridView1.DataSource = ds;
GridView1.DataBind();

}

}

//This code is for checking the file extension
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case “.gif”:
return true;
case “.png”:
return true;
case “.jpg”:
return true;
case “.jpeg”:
return true;
default:
return false;
}
}
}

Posted in XML. 8 Comments »

How to insert data in XML file using LINQ to XML?


Step1:Create one class i.e Emp.cs and declare the property like this

public class Emp
{
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpSal { get; set; }
}

Step2: Create the Emp.xml file like this in Solution Explore

<?xml version="1.0" encoding="utf-8"?>
<Emps>

</Emps>

Step3: Write the code in code behind like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

public partial class TestXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("Emp.xml"));

XElement newEmp = new XElement("Emp",
new XElement("EmpName", TextBox1.Text),
new XElement("EmpSal", TextBox2.Text)
);
newEmp.SetAttributeValue("Id", GetNextAvailableID());

data.Element("Emps").Add(newEmp);
data.Save(HttpContext.Current.Server.MapPath("Emp.xml"));

TextBox1.Text = "";
TextBox2.Text = "";

FillGrid();

}

private static int GetNextAvailableID()
{
XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("Emp.xml"));

return Convert.ToInt32(
(from c in data.Descendants("Emp")
orderby Convert.ToInt32(c.Attribute("Id").Value) descending
select c.Attribute("Id").Value).FirstOrDefault()
) + 1;
}

protected void FillGrid()
{
var query = from a in XElement.Load(MapPath("Emp.xml")).Elements("Emp")
select new Emp
{

Id = Convert.ToInt32(a.Attribute("Id").Value),
EmpName = (string)a.Element("EmpName"),
EmpSal = (string)a.Element("EmpSal")

};

GridView1.DataSource = query;
GridView1.DataBind();
}
}

Posted in LINQ, XML. 2 Comments »

How to fill Grid from XML file ?


Hi

For doing this task create a Emp.xml file like this

<?xml version=”1.0″ encoding=”utf-8″?>
<Emps>
<Emp Id=”1″>
<EmpName>werwer</EmpName>
<EmpSal>345345</EmpSal>
</Emp>
<Emp Id=”2″>
<EmpName>Ram</EmpName>
<EmpSal>45000</EmpSal>
</Emp>
<Emp Id=”3″>
<EmpName>Hari</EmpName>
<EmpSal>35000</EmpSal>
</Emp>
</Emps>

Step2: Write the code in Code behind file like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class GridFillXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Server.MapPath(“Emp.xml”);
using (DataSet ds = new DataSet())
{
ds.ReadXml(filePath);
GridView1.DataSource = ds;
GridView1.DataBind();
}

}
}