I'm new in using Xpath with ASP.NET C#. I have a Gridview and am able to Insert/Edit/Delete data from an xml file. However I need to be able to perform the same task using a more complicated xml file. Could someone help me get started as to how I would go about implementing the Xpath Functionality into my code?
我是使用Xpath和ASP.NET C#的新手。我有一个Gridview,能够从xml文件中插入/编辑/删除数据。但是,我需要能够使用更复杂的xml文件执行相同的任务。有人可以帮助我开始如何在我的代码中实现Xpath功能吗?
using the following xml ex.1, all functions as expected:
使用以下xml ex.1,所有函数按预期方式:
<root>
<partNumbers>
<partid>0</partid>
<partnumber>796542</partnumber>
</partNumbers>
<partNumbers>
<partid>1</partid>
<partnumber>225614</partnumber>
</partNumbers>
<partNumbers>
<partid>2</partid>
<partnumber>123457</partnumber>
</partNumbers>
</root>
...however, I need to perform the same actions using the following xml ex.2 file, Note: The actual file consist of more, but am just using the following for testing:
...但是,我需要使用以下xml ex.2文件执行相同的操作,注意:实际文件包含更多,但我只是使用以下内容进行测试:
<root>
<pos>
<partNumbers>
<partid>0</partid>
<partnumber>796542</partnumber>
</partNumbers>
<partNumbers>
<partid>1</partid>
<partnumber>225614</partnumber>
</partNumbers>
<partNumbers>
<partid>2</partid>
<partnumber>123457</partnumber>
</partNumbers>
</pos>
</root>
...Code behind - I've included the header directives System.Xml and System.Xml.Xpath (snippet)
...代码背后 - 我已经包含了头部指令System.Xml和System.Xml.Xpath(snippet)
protected void BindGridView()
{
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
gvPartNumber.DataSource = dsgvPartNumber;
gvPartNumber.DataBind();
gvPartNumber.ShowFooter = true;
}
...the rest are just my functions for insert/edit/ delete, etc, which works fine
...其余的只是我的插入/编辑/删除等功能,工作正常
.....The Gridview:
<asp:GridView ID="gvPartNumber" runat="server" AutoGenerateColumns="False"
onrowdeleting="gvPartNumber_RowDeleting" onrowediting="gvPartNumber_RowEditing"
onrowupdating="gvPartNumber_RowUpdating" onrowcommand="gvPartNumber_RowCommand"
ShowFooter="True" Width="482px"
onrowcancelingedit="gvPartNumber_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="Part ID">
<EditItemTemplate>
<asp:TextBox ID="txtPartID" runat="server" Text='<%# Bind("partID") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPartIDInsert" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblPartID" runat="server" Text='<%# Bind("PartID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Part Number">
<EditItemTemplate>
<asp:TextBox ID="txtPartNumber" runat="server" Text='<%# Bind("partNumber") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPartNumberInsert" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblPartNumber" runat="server" Text='<%# Bind("PartNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnInsert" runat="server" CommandName="insertXMLData">Insert</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="120px" />
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
...when I run the page using ex.2 - I get a error: System.Data.DataRowView' does not contain a property with the name 'PartID - Could someone please provide me with some direction as to how I would go about using Xpath to resolve this issue?
...当我使用ex.2运行页面时 - 我收到一个错误:System.Data.DataRowView'不包含名为'PartID的属性 - 有人可以告诉我一些关于如何去做的方向使用Xpath解决此问题?
1 个解决方案
#1
0
It will work if you change
如果你改变它会工作
gvPartNumber.DataSource = dsgvPartNumber;
to
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
Probably you have overlooked the second xml is returning two tables in the dataset which is normal behavior. When you are setting your gridview source to this dataset, it is using default table (pos) to bind and data binding fails.
可能你忽略了第二个xml返回数据集中的两个表,这是正常的行为。当您将gridview源设置为此数据集时,它使用默认表(pos)进行绑定,并且数据绑定失败。
#1
0
It will work if you change
如果你改变它会工作
gvPartNumber.DataSource = dsgvPartNumber;
to
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
Probably you have overlooked the second xml is returning two tables in the dataset which is normal behavior. When you are setting your gridview source to this dataset, it is using default table (pos) to bind and data binding fails.
可能你忽略了第二个xml返回数据集中的两个表,这是正常的行为。当您将gridview源设置为此数据集时,它使用默认表(pos)进行绑定,并且数据绑定失败。