Sub getData()
If Not IsPostBack Then
bindData()
End If
End Sub
Sub bindData()
Dim ds As New DataSet()
Dim r As New XmlTextReader(Server.MapPath("~/app_data/carSpec.xml"))
r.MoveToContent()
r.ReadStartElement("Car")
ds.ReadXml(r)
dataGrid.DataSource = ds
dataGrid.DataBind()
End Sub
Protected Sub dataGrid_RowEditing(sender As Object, e As GridViewEditEventArgs)
dataGrid.EditIndex = e.NewEditIndex
bindData()
End Sub
Protected Sub dataGrid_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
dataGrid.EditIndex = -1
bindData()
End Sub
Protected Sub dataGrid_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim i As Integer = dataGrid.Rows(e.RowIndex).DataItemIndex
Dim name As String = DirectCast(dataGrid.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text
Dim value As String = DirectCast(dataGrid.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
dataGrid.EditIndex = -1
bindData()
Dim ds As DataSet = DirectCast(dataGrid.DataSource, DataSet)
ds.Tables(0).Rows(i)("name") = name
ds.Tables(0).Rows(i)("value") = value
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End Sub
Protected Sub dataGrid_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
bindData()
Dim ds As DataSet = DirectCast(dataGrid.DataSource, DataSet)
ds.Tables(0).Rows(dataGrid.Rows(e.RowIndex).DataItemIndex).Delete()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End Sub
Protected Sub dataGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "insert" Then
Dim name As String = DirectCast(dataGrid.FooterRow.FindControl("txtName"), TextBox).Text
Dim value As String = DirectCast(dataGrid.FooterRow.FindControl("txtvalue"), TextBox).Text
bindData()
Dim ds As DataSet = TryCast(dataGrid.DataSource, DataSet)
Dim dr As DataRow = ds.Tables(0).NewRow()
dr(0) = name
dr(1) = value
ds.Tables(0).Rows.Add(dr)
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End If
End Sub
I change the bindGrid sub in that example and it displays the data but when inserting and updating i get object reference error:
Sub BindGrid()
Dim docReader As New XmlDocument
docReader.Load(Server.MapPath("~/XMLFile.xml"))
Dim xnl As XmlNodeList
xnl = docReader.SelectNodes("Mitarbeiter/test")
Dim docDataSet As New DataSet
For Each xn As XmlNode In xnl
Dim xr As New XmlNodeReader(xn)
docDataSet.ReadXml(xr)
GridView1.DataSource = docDataSet
GridView1.DataBind()
Next xn
End Sub
this is the first time i see that ! is sth not polite!
anyway
i am really confused what i see is the complete code
for example thois the is insert to gridview code
Protected Sub dataGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "insert" Then
Dim name As String = DirectCast(dataGrid.FooterRow.FindControl("txtName"), TextBox).Text
Dim value As String = DirectCast(dataGrid.FooterRow.FindControl("txtvalue"), TextBox).Text
bindData()
Dim ds As DataSet = TryCast(dataGrid.DataSource, DataSet)
Dim dr As DataRow = ds.Tables(0).NewRow()
dr(0) = name
dr(1) = value
ds.Tables(0).Rows.Add(dr)
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End If
End Sub
You should first complete all the inserting values and then do updating.
PS: You should save your DataSet directly into the ViewState, and then do modifications by converting from ViewState to DataTable and then write back to the file or memory.
Protected Sub dataGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "insert" Then
Dim name As String = DirectCast(dataGrid.FooterRow.FindControl("txtName"), TextBox).Text
Dim value As String = DirectCast(dataGrid.FooterRow.FindControl("txtvalue"), TextBox).Text
Dim ds As DataSet = CType(ViewState("ds"),DataSet)
Dim dr As DataRow = ds.Tables(0).NewRow()
dr(0) = name
dr(1) = value
ds.Tables(0).Rows.Add(dr)
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End If
End Sub
keyvan1
Member
86 Points
272 Posts
get xml nested element by dataset
Jan 21, 2013 06:29 PM|LINK
i have a gridview and an xml file
the format of xml file is nested like below.How can i change the code to update/edit the nested element
Car/bodyStyles
i changed the code and it works and displays the data but it does not update or delete or insert!!!
and second if i add another node like fuelTypes how can i get those elements in that node
<fuelTypes>
<item name="any" value="any" />
<item name="test" value="test" />
</fuelTypes>
Sub getData()
If Not IsPostBack Then
bindData()
End If
End Sub
Sub bindData()
Dim ds As New DataSet()
Dim r As New XmlTextReader(Server.MapPath("~/app_data/carSpec.xml"))
r.MoveToContent()
r.ReadStartElement("Car")
ds.ReadXml(r)
dataGrid.DataSource = ds
dataGrid.DataBind()
End Sub
Protected Sub dataGrid_RowEditing(sender As Object, e As GridViewEditEventArgs)
dataGrid.EditIndex = e.NewEditIndex
bindData()
End Sub
Protected Sub dataGrid_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
dataGrid.EditIndex = -1
bindData()
End Sub
Protected Sub dataGrid_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim i As Integer = dataGrid.Rows(e.RowIndex).DataItemIndex
Dim name As String = DirectCast(dataGrid.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text
Dim value As String = DirectCast(dataGrid.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
dataGrid.EditIndex = -1
bindData()
Dim ds As DataSet = DirectCast(dataGrid.DataSource, DataSet)
ds.Tables(0).Rows(i)("name") = name
ds.Tables(0).Rows(i)("value") = value
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End Sub
Protected Sub dataGrid_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
bindData()
Dim ds As DataSet = DirectCast(dataGrid.DataSource, DataSet)
ds.Tables(0).Rows(dataGrid.Rows(e.RowIndex).DataItemIndex).Delete()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End Sub
Protected Sub dataGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "insert" Then
Dim name As String = DirectCast(dataGrid.FooterRow.FindControl("txtName"), TextBox).Text
Dim value As String = DirectCast(dataGrid.FooterRow.FindControl("txtvalue"), TextBox).Text
bindData()
Dim ds As DataSet = TryCast(dataGrid.DataSource, DataSet)
Dim dr As DataRow = ds.Tables(0).NewRow()
dr(0) = name
dr(1) = value
ds.Tables(0).Rows.Add(dr)
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End If
End Sub
<?xml version="1.0" encoding="utf-8"?>
<Car>
<bodyStyles>
<item name="any" value="any" />
<item name="test" value="test" />
<item name="test2" value="test2" />
</bodyStyles>
</Car>
<asp:GridView ID="dataGrid" runat="server" AutoGenerateColumns="False"
onrowdeleting="dataGrid_RowDeleting" onrowediting="dataGrid_RowEditing"
onrowupdating="dataGrid_RowUpdating" onrowcommand="dataGrid_RowCommand"
ShowFooter="True" Width="482px"
onrowcancelingedit="dataGrid_RowCancelingEdit" CssClass="table" CellPadding="0" CellSpacing="0" >
<Columns>
<asp:TemplateField HeaderText='<%$ Resources:admin,Data %>'>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText='<%$ Resources:admin,value %>'>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("value") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("value") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text='<%$ Resources:admin,update %>'/>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text='<%$ Resources:admin,cancel %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnInsert" runat="server" CommandName="insert" Text='<%$ Resources:admin,add %>' />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text='<%$ Resources:admin,edit %>' />
</ItemTemplate>
<ItemStyle Width="120px" />
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: get xml nested element by dataset
Jan 23, 2013 01:52 AM|LINK
Hi,
For using GridView to update the certain node's Value, there's no direct way.
But you can consider:
1) Just use DataTable's WriteXml/ReadXml from certain path, and then do modifying directly through the DataTable.Rows[index][index].
2) Just use XDocument or XmlDocument to analyze the xml structures and then do modifications.
3) For more you can refer this sample:
http://www.codeproject.com/Articles/18280/Edit-XML-files-using-a-GridView
keyvan1
Member
86 Points
272 Posts
Re: get xml nested element by dataset
Jan 23, 2013 08:58 AM|LINK
I change the bindGrid sub in that example and it displays the data but when inserting and updating i get object reference error:
Sub BindGrid()
Dim docReader As New XmlDocument
docReader.Load(Server.MapPath("~/XMLFile.xml"))
Dim xnl As XmlNodeList
xnl = docReader.SelectNodes("Mitarbeiter/test")
Dim docDataSet As New DataSet
For Each xn As XmlNode In xnl
Dim xr As New XmlNodeReader(xn)
docDataSet.ReadXml(xr)
GridView1.DataSource = docDataSet
GridView1.DataBind()
Next xn
End Sub
<?xml version="1.0" standalone="yes"?>
<Mitarbeiter>
<test>
<ma>
<id>1</id>
<name>Florian 1</name>
<phone>345</phone>
</ma>
<ma>
<id>4</id>
<name>Hans Meier</name>
<phone>blablabl</phone>
</ma>
<ma>
<id>6</id>
<name>Franzy</name>
<phone>3456</phone>
</ma>
<ma>
<id>7</id>
<name>Fritz</name>
<phone>345</phone>
</ma>
<ma>
<id>8</id>
<name>Hugo</name>
<phone>345</phone>
</ma>
<ma>
<id>9</id>
<name>Eugen</name>
<phone>345</phone>
</ma>
<ma>
<id>0</id>
<name>Hello</name>
<phone>World</phone>
</ma>
<ma>
<id>32</id>
<name>c</name>
<phone>b</phone>
</ma>
<ma>
<id>32</id>
<name>c</name>
<phone>b</phone>
</ma>
<ma>
<id>5</id>
<name>55</name>
<phone>5</phone>
</ma>
</test>
</Mitarbeiter>
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: get xml nested element by dataset
Jan 24, 2013 12:25 AM|LINK
How do you inserting and updating?
Codes please.
keyvan1
Member
86 Points
272 Posts
Re: get xml nested element by dataset
Jan 24, 2013 06:38 AM|LINK
The codes are in my first qustion post right up here!!!!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: get xml nested element by dataset
Jan 24, 2013 07:25 AM|LINK
Hi,
First please don't use "!", this isn't very polite to others;)
Then I have to tell you that your first post is only telling me how to read xml contents instead of inserting or updating.
keyvan1
Member
86 Points
272 Posts
Re: get xml nested element by dataset
Jan 24, 2013 08:06 AM|LINK
this is the first time i see that ! is sth not polite!
anyway
i am really confused what i see is the complete code
for example thois the is insert to gridview code
Protected Sub dataGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "insert" Then
Dim name As String = DirectCast(dataGrid.FooterRow.FindControl("txtName"), TextBox).Text
Dim value As String = DirectCast(dataGrid.FooterRow.FindControl("txtvalue"), TextBox).Text
bindData()
Dim ds As DataSet = TryCast(dataGrid.DataSource, DataSet)
Dim dr As DataRow = ds.Tables(0).NewRow()
dr(0) = name
dr(1) = value
ds.Tables(0).Rows.Add(dr)
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml"))
bindData()
End If
End Sub
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: get xml nested element by dataset
Jan 24, 2013 08:16 AM|LINK
Hi again,
You should first complete all the inserting values and then do updating.
PS: You should save your DataSet directly into the ViewState, and then do modifications by converting from ViewState to DataTable and then write back to the file or memory.
Protected Sub dataGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs) If e.CommandName = "insert" Then Dim name As String = DirectCast(dataGrid.FooterRow.FindControl("txtName"), TextBox).Text Dim value As String = DirectCast(dataGrid.FooterRow.FindControl("txtvalue"), TextBox).Text Dim ds As DataSet = CType(ViewState("ds"),DataSet) Dim dr As DataRow = ds.Tables(0).NewRow() dr(0) = name dr(1) = value ds.Tables(0).Rows.Add(dr) ds.AcceptChanges() ds.WriteXml(Server.MapPath("~/app_data/carSpec.xml")) bindData() End If End Sub