Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML - write

Posted on 2006-11-09
12
Medium Priority
?
306 Views
Last Modified: 2006-11-18
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.NewLineOnAttributes = 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
0
Comment
Question by:VBdotnet2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17912232
We'd need to see the page you're trying to use to achieve this as well.
0
 

Author Comment

by:VBdotnet2005
ID: 17912250
XML page ? I don't understand.
0
 

Author Comment

by:VBdotnet2005
ID: 17912261
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()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:muzzy2003
ID: 17912334
Not the XML page, the ASP.NET page. I need to see exactly how and where the Account and RepID controls are defined.
0
 

Author Comment

by:VBdotnet2005
ID: 17912352
this one?

<asp:Button ID="btnAdd" runat="server" Style="z-index: 101; left: 250px; position: absolute;
            top: 122px" Text="Add" />
        &nbsp;
        <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>
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17912371
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?
0
 

Author Comment

by:VBdotnet2005
ID: 17912380
It is being overwritten rather than appened to.
0
 

Author Comment

by:VBdotnet2005
ID: 17912391
This message pops up when I click Add .

-------This file has been modified outside of the source editor. Do you want to reload it? ----------------
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17912410
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.
0
 

Author Comment

by:VBdotnet2005
ID: 17912483
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?
0
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 1000 total points
ID: 17912526
OK. You can append elements to an XML file, but not using the XmlWriter you are using, as this recreates the file each time. You will need a structure more like this:

<?xml version="1.0" encoding="utf-8"?>
<Users>
  <UserID>
    <Account>12345</Account>
    <ID>A01</ID>
  </UserID>
  <UserID>
    <Account>98765</Account>
    <ID>A02</ID>
  </UserID>
</Users>

To achieve it, do the following:

Dim xmlfile As String = Server.MapPath("UserID.xml")
Dim xml As XmlDocument = New XmlDocument()
xml.Load(xmlfile)
Dim root As XmlNode = xml.SelectSingleNode("/Users")
Dim node As XmlNode = root.AppendChild(xml.CreateElement("UserID"));
node.AppendChild(xml.CreateElement("Account")).InnerText = Me.Account.Text
node.AppendChild(xml.CreateElement("ID")).InnerText = Me.RepID.Text
xml.Save(xmlfile)

This is typed blind (no VS to hand), so post any issues with it and I'll sort them out.
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17912541
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.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question