简体   繁体   中英

Filtering and binding xml data to gridview in Asp.net c#

I got a web application which contains a XML file for storing user data.I want to display entered data in grid view for that specific user only based on 'txtEmpid.Text'. I think the issue is with string reader. But I can't seem to figure out what as I am new to asp.net and this code gives me blank page. Thanks for help

My XML file:

-<SkillSet>

    -<SkillSets>

    <Employee_ID>1</Employee_ID>

    <Employee_Name>abc</Employee_Name>

    <PL_Name>xyz</PL_Name>

    <Skill_Name1>C# with Asp.NET</Skill_Name1>

    <Skill_Type1>Programming</Skill_Type1>

    <Skill_Proficiency1>Beginner</Skill_Proficiency1>

    <Experience1>1</Experience1>

    <Skill_Name2>FL Studio</Skill_Name2>

    <Skill_Type2>Others</Skill_Type2>

    <Skill_Proficiency2>Intermediate</Skill_Proficiency2>

    <Experience2>2</Experience2>

    <Skill_Name3>ms word</Skill_Name3>

    <Skill_Type3>others</Skill_Type3>

    <Skill_Proficiency3>Advance</Skill_Proficiency3>

    <Experience3>3</Experience3>

    <Skill_Name4>Camtasia</Skill_Name4>

    <Skill_Type4>Others</Skill_Type4>

    <Skill_Proficiency4>Professional</Skill_Proficiency4>

    <Experience4>4</Experience4>

    <Skill_Name5>MS excel</Skill_Name5>

    <Skill_Type5>Programming</Skill_Type5>

    <Skill_Proficiency5>Beginner</Skill_Proficiency5>

    <Experience5>5</Experience5>

    <Comments>fgfdgdf</Comments>

    </SkillSets>

    </SkillSet>

and here is my C# file that has logic to bind data :

 private void BindGrid()
{
    try {

        var xmlStr = File.ReadAllText(Server.MapPath("~/SkillSet.xml"));
        var str = XElement.Parse(xmlStr);
        var result = str.Elements("SkillSets").
        Where(x => x.Element("Employee_ID").Value.Equals(txtEmpid.Text)).ToList();

        StringReader theReader = new StringReader(result.ToString());

        DataSet ds = new DataSet();
        ds.ReadXml(theReader);
        if (ds != null && ds.HasChanges())
        {
            grdxml.DataSource = ds;
            grdxml.DataBind();

        }
        else
        {

            grdxml.DataBind();

        }
    }

    catch(Exception ex)
    {
        lblerror.Text = ex.ToString();
    }

try this:

    string filePath = "Complete path where you saved the XML file";     
    DataSet ds = new DataSet();
    ds.ReadXml(filePath);    
    var skillSets = ds.Tables[0].AsEnumerable();
    var query = from skillset in skillSets
                where skillset.Field<string>("Employee_ID") == "1" // here you can get value from text box etc.
                select skillset;
    grdxml.DataSource = query.ToList();
    grdxml.DataBind();

Your LINQ query is good. The problem in your code is here:

StringReader theReader = new StringReader(result.ToString());
ds.ReadXml(theReader);

You're using result.ToString() , and the ToString() representation of a List of XElements is nothing you can read as XML. In fact, calling ToString() will result in this

"System.Collections.Generic.List`1[System.Xml.Linq.XElement]"

And as you can see, that's not XML

For your code to work, try something like this:

 try {

     var xmlStr = File.ReadAllText(Server.MapPath("~/SkillSet.xml"));
     var str = XElement.Parse(xmlStr);
     var result = str.Elements("SkillSets").
     Where(x => x.Element("Employee_ID").Value.Equals(txtEmpid.Text)).ToList();

     grdxml.DataSource = result.ToList();
     grdxml.DataBind();
 }  
 catch(Exception ex)
 {
    lblerror.Text = ex.ToString();
 }

Also, remove the slashes on your XML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM