WriteXML method of dataset object

Results 1 to 4 of 4

Thread: WriteXML method of dataset object

  1. #1
    Join Date
    Dec 1969

    Default WriteXML method of dataset object

    i am using the writexml method of a dataset object, and it is writing, but the problem is that it re-writes the preceding xml record every time it is called to write a new record. this makes it absolutely useless!<BR><BR>can anyone show me how to fix my code so that it maintains the old records when it writes each new record? the xml file should serve as a datastore that allows me to record process information on an ongoing basis without the overhead of writing to sql server.<BR><BR>here is the code for creating the dataset and then writing to the xml file:<BR>-----------------------------------------------------------------<BR>Imports various namespaces (omitted 4 brevity)<BR><BR>Public Class XMLtraffic<BR>Inherits Page<BR><BR> Sub Browser_Data()<BR> &#039; declare & populate all the variables<BR> Dim Referer As String = Request.ServerVariables("HTTP_REFERER")<BR> IF Referer = "" THEN<BR> Referer = "direct"<BR> END IF<BR>&#039; repeat for various other variables<BR><BR> &#039; create a dataset object<BR> Dim userinfoDS As DataSet = New DataSet("UserInformation")<BR><BR> &#039; create a table & add columns to it for each variable<BR> Dim browserTable As DataTable = userinfoDS.Tables.Add("BrowserInfo")<BR><BR> Dim pkCol As DataColumn = browserTable.Columns.Add("Referer", Type.GetType("System.String"))<BR> browserTable.Columns.Add("BrowserType", Type.GetType("System.String"))<BR>&#039; repeat for all the other variables, 1 column per variable<BR><BR> browserTable.PrimaryKey = New DataColumn() {pkCol} <BR><BR> &#039; create & insert a row into the dataset&#039;s browserTable<BR> Dim myDataRow As DataRow = browserTable.NewRow()<BR> myDataRow("Referer") = Referer<BR> myDataRow("BrowserType") = BrowserType<BR>&#039; repeat to add a column for every variable<BR> browserTable.Rows.Add(myDataRow) <BR><BR> Dim sw As StreamWriter = new StreamWriter(Server.MapPath("XML/BrowserInfo.xml"))<BR> userinfoDS.WriteXml(sw)<BR> sw.Close()<BR> End Sub<BR>End Class<BR>-----------------------------------------------------------------<BR>the xml output in the xml file looks like this:<BR>-----------------------------------------------------------------<BR>&#060;UserInformation&#062;<BR> &#060;BrowserInfo&#062;<BR> &#060;Referer&#062;direct&#060;/Referer&#062;<BR> &#060;BrowserType&#062;IE5&#060;/BrowserType&#062;<BR>&#039; a number of other elements within BrowserInfo, 1 element per variable<BR> &#060;/BrowserInfo&#062;<BR>&#060;/UserInformation&#062;<BR>-----------------------------------------------------------------<BR>as you can see, the UserInformation element should contain a number of BrowserInfo elements, each with its own collection of sub-elements. Instead, however, the WriteXML method of the ado recordset object seems to be over-writing the same BrowserInfo element again and again, so that there is only 1 BrowserInfo element ever. when i test this from my machine, i notice that the the values that should change when i run this sub from various pages do in fact change in the xml file. so the script is indeed running from multiple pages and is in fact writing to the file again and again from various contexts. but it is erasing the old content when it writes each new time, which is unnacceptable.<BR><BR>can someone please show me what i am doing wrong?<BR><BR>i feared at first that perhaps it was a problem to be setting referrer as a primary key in the ado recordset, but then i realized that shouldnt matter because ado.net is disconnected from the xml file, and because ado.net is only handling 1 record each time it is called into action

  2. #2

    Default RE: WriteXML method of dataset object

    You are not loading the existing data.<BR>You create a DS with one row and save it as xml (overwriting the existing file).

  3. #3
    Join Date
    Dec 1969

    Default so what do i do then?

    ok, so how do i change my code to fix the problem?

  4. #4

    Default RE: so what do i do then?

    That&#039;s up to you, you can load the XML into a DOM and add annother node at the end.<BR><BR>You could append Text (aka xml tags) to the end of a text file.<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts