将特定记录从xml文件过滤到gridview asp.net

时间:2017-01-12 15:28:04

标签: c# asp.net xml linq datagridview

我有一个包含员工数据的xml文件。我的页面的设计是这样的,当用户输入他的ID时,gridview显示他的技能(如果已经存在),否则继续输入他的数据(新员工)。我想使用他的ID过滤记录员工的数据并在网格视图中显示。但是在使用我的代码时,它会引发异常声明"  System.Data.DataRow'不包含名称为' EmployeeID'"的属性。我已经用正确的值限制了模板。那似乎是什么问题呢?

我的xml架构

public class StudentAdapter extends ArrayAdapter<Student> {

    private Activity context;
    private ArrayList<Student> students;
    TextView idText;
    TextView nameText;

    public StudentAdapter(Activity context, ArrayList<Student> student) {
        super(context, R.layout.student_info, student);
        this.context = context;
        this.students = student;
    }

    //Array Adapter
    public View getView(int position, View rowView, ViewGroup parent){

        LayoutInflater layoutInflater = context.getLayoutInflater();
        rowView = layoutInflater.inflate(R.layout.student_info, parent, false);
        idText = (TextView) rowView.findViewById(R.id.idText);
        nameText = (TextView) rowView.findViewById(R.id.nameText);

        idText.setText(students.get(position).getId()+"");
        nameText.setText(String.valueOf(students.get(position).getName()));

        return rowView;
    }
}

我的aspx页面源代码:

-<SkillSet>


-<SkillSets>

<EmployeeID>1</EmployeeID>

<EmployeeName>abc</EmployeeName>

<PLName>xyz</PLName>

<SkillName1>Html</SkillName1>

<SkillType1>Programming</SkillType1>

<SkillProficiency1>Beginner</SkillProficiency1>

<Experience1>1</Experience1>

<Comments>Hello</Comments>

</SkillSets>

</SkillSet>

我的c#绑定网格代码:

<asp:GridView ID="grdxml" runat="server" AutoGenerateColumns="False" BackColor="White"
                        BorderColor="Black" BorderStyle="None" BorderWidth="1px" CellPadding="1" GridLines="Vertical"
                        OnSelectedIndexChanged="grdxml_SelectedIndexChanged" 
                        onrowdeleting="grdxml_RowDeleting">
                        <AlternatingRowStyle BackColor="#DCDCDC" />
                        <Columns>
                            <asp:TemplateField HeaderText="ID">
                                <ItemTemplate>
                                    <asp:Label ID="lblEmpId" runat="server" Text='<%# Bind("EmployeeID")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblEmpName" runat="server" Text='<%# Bind("EmployeeName")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="PL Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblPLName" runat="server" Text='<%# Bind("PLName")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Skill Name 1">
                                <ItemTemplate>
                                    <asp:Label ID="lblSkillName1" runat="server" Text='<%# Bind("SkillName1")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Skill Type 1">
                                <ItemTemplate>
                                    <asp:Label ID="lblSkillType1" runat="server" Text='<%# Bind("SkillType1")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Skill Proficiency 1">
                                <ItemTemplate>
                                    <asp:Label ID="lblSkillProf1" runat="server" Text='<%# Bind("SkillProficiency1")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Experience">
                                <ItemTemplate>
                                    <asp:Label ID="lblExp1" runat="server" Text='<%# Bind("Experience1")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
 <asp:TemplateField HeaderText="Command"> <ItemTemplate>
                                    <asp:LinkButton Text="Select" ID="lnkSelect" runat="server" CommandName="Select" />
                                     <asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete">Delete</asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>                     
                    </asp:GridView>

完整堆栈跟踪:

private void BindGrid()
    {

        string filename = Server.MapPath("~/SkillSet.xml");

        if (File.Exists(filename) == true)
        {
            try
            {

                string filePath = Server.MapPath("SkillSet.xml");
                DataSet ds = new DataSet();
                ds.ReadXml(filePath);
                var skillSets = ds.Tables[0].AsEnumerable();
                var query = from SkillSets in skillSets
                            where SkillSets.Field<string>("EmployeeID") == emplid
                            select SkillSets;
                grdxml.DataSource = query.ToList();
                grdxml.DataBind();
            }
            catch (Exception ex)
            {
                lblerror.Text = ex.ToString();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我刚测试了这个并且工作正常:

var xmlStr = File.ReadAllText(Server.MapPath("YourFileName.xml"));
var str = XElement.Parse(xmlStr);
var result = str.Elements("SkillSets").Where(x => x.Element("EmployeeID").Value.Equals(emplid.ToString())).ToList();    
List<Employee> mapList = new List<Employee>();
foreach (var item in result)
{
    Employee obj = new Employee();
    obj.EmployeeID = item.Element("EmployeeID").Value;
    obj.EmployeeName = item.Element("EmployeeName").Value;
    obj.PLName = item.Element("PLName").Value;
    obj.SkillName1 = item.Element("SkillName1").Value;
    obj.SkillType1 = item.Element("SkillType1").Value;
    obj.SkillProficiency1 = item.Element("SkillProficiency1").Value;
    obj.Experience1 = item.Element("Experience1").Value;
    obj.Comments = item.Element("Comments").Value;
    mapList.Add(obj);
}

grdxml.DataSource = mapList;
grdxml.DataBind();

您可以创建Employee的类:

public class Employee
    {
    private string _EmployeeID;
    public string EmployeeID
    {
        get
        {
            return _EmployeeID;
        }
        set
        {
            _EmployeeID = value;
        }

    }
    private string _EmployeeName;
    public string EmployeeName
    {
        get
        {
            return _EmployeeName;
        }
        set
        {
            _EmployeeName = value;
        }
    }
    private string _PLName;
    public string PLName
    {
        get
        {
            return _PLName;
        }
        set
        {
            _PLName = value;
        }
    }

    private string _SkillName1;
    public string SkillName1
    {
        get
        {
            return _SkillName1;
        }
        set
        {
            _SkillName1 = value;
        }
    }

    private string _SkillType1;
    public string SkillType1
    {
        get
        {
            return _SkillType1;
        }
        set
        {
            _SkillType1 = value;
        }
    }

    private string _SkillProficiency1;
    public string SkillProficiency1
    {
        get
        {
            return _SkillProficiency1;
        }
        set
        {
            _SkillProficiency1 = value;
        }
    }

    private string _Experience1;
    public string Experience1
    {
        get
        {
            return _Experience1;
        }
        set
        {
            _Experience1 = value;
        }
    }

    private string _Comments;
    public string Comments
    {
        get
        {
            return _Comments;
        }
        set
        {
            _Comments = value;
        }
    }
}