VBdotnet2005
asked on
XML - write
When I click Add, why Account and RepID are not appended to xmlfile?
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim xmlfile As String = Server.MapPath("UserID.xml ")
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
settings.NewLineOnAttribut es = True
Using writer As XmlWriter = XmlWriter.Create(xmlfile, Nothing)
writer.WriteStartDocument( )
writer.WriteStartElement(" Userinfo")
writer.WriteElementString( "Account", Me.Account.Text)
writer.WriteElementString( "RepID", Me.RepID.Text)
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
End Sub
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim xmlfile As String = Server.MapPath("UserID.xml
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
settings.NewLineOnAttribut
Using writer As XmlWriter = XmlWriter.Create(xmlfile, Nothing)
writer.WriteStartDocument(
writer.WriteStartElement("
writer.WriteElementString(
writer.WriteElementString(
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
End Sub
We'd need to see the page you're trying to use to achieve this as well.
ASKER
XML page ? I don't understand.
ASKER
I am just trying to display on Gridview.
Dim xmlfile As String = Server.MapPath(".")
Dim ds As New DataSet
ds.ReadXmlSchema(xmlfile & "\userID.xsd")
ds.ReadXml(xmlfile & "\userID.xml")
Me.GridView1.DataSource = ds
Me.GridView1.DataBind()
Dim xmlfile As String = Server.MapPath(".")
Dim ds As New DataSet
ds.ReadXmlSchema(xmlfile & "\userID.xsd")
ds.ReadXml(xmlfile & "\userID.xml")
Me.GridView1.DataSource = ds
Me.GridView1.DataBind()
Not the XML page, the ASP.NET page. I need to see exactly how and where the Account and RepID controls are defined.
ASKER
this one?
<asp:Button ID="btnAdd" runat="server" Style="z-index: 101; left: 250px; position: absolute;
top: 122px" Text="Add" />
<asp:TextBox ID="txtAccount" runat="server" Style="z-index: 102; left: 188px; position: absolute;
top: 46px"></asp:TextBox>
<asp:TextBox ID="txtRepID" runat="server" Style="z-index: 103; left: 188px; position: absolute;
top: 86px"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Style="z-index: 104; left: 67px; position: absolute;
top: 53px" Text="Account : "></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 69px; position: absolute;
top: 89px" Text="RepID : "></asp:Label>
<asp:GridView ID="GridView1" runat="server" Style="z-index: 107; left: 29px; position: absolute;
top: 195px" Width="322px">
</asp:GridView>
</div>
<asp:Button ID="btnAdd" runat="server" Style="z-index: 101; left: 250px; position: absolute;
top: 122px" Text="Add" />
<asp:TextBox ID="txtAccount" runat="server" Style="z-index: 102; left: 188px; position: absolute;
top: 46px"></asp:TextBox>
<asp:TextBox ID="txtRepID" runat="server" Style="z-index: 103; left: 188px; position: absolute;
top: 86px"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Style="z-index: 104; left: 67px; position: absolute;
top: 53px" Text="Account : "></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 69px; position: absolute;
top: 89px" Text="RepID : "></asp:Label>
<asp:GridView ID="GridView1" runat="server" Style="z-index: 107; left: 29px; position: absolute;
top: 195px" Width="322px">
</asp:GridView>
</div>
OK. Is the problem that your file is being overwritten rather than appened to? Or are you just getting a <Userinfo/> element with nothing within it?
ASKER
It is being overwritten rather than appened to.
ASKER
This message pops up when I click Add .
-------This file has been modified outside of the source editor. Do you want to reload it? ----------------
-------This file has been modified outside of the source editor. Do you want to reload it? ----------------
The message is expected - the file has been changed. You are expecting multiple Userinfo elements but only have the most recent one is what I am guessing. Can you post the Xml file you have, and the Xml file you want? I can then post some code that will do what you want.
ASKER
For example, just a simple one. Maybe I don't really understand how to add more record on XML file. What I am trying to say is that we can add records to a table by using Insert statement. We can't do the same(or similair) with XML? Or the size of the file remains the same.
<?xml version="1.0" encoding="utf-8"?>
<UserID>
<Account>12345</Account>
<ID>A01</ID>
</UserID>
Why can't I add more Account and ID and display on Gridview?
<?xml version="1.0" encoding="utf-8"?>
<UserID>
<Account>12345</Account>
<ID>A01</ID>
</UserID>
Why can't I add more Account and ID and display on Gridview?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Should have said - you'll need to save the XML file with this Users element (or whatever you choose to call it) in place first, or this code will fall over. I could write code to cater for it not being there, but this would create an overhead each time the method is called, so given that this is a one-off task, best do it manually.