G
Guest
I am trying to insert a node into an XMLFile. using XMLTextwriter. My
Question is
Is it possible to do without using XMLDocument. Because its loading all the
the file into memory. I just want to insert in the front. My code is give
below.
Is it possible to do without using XMLDOcument?
Dim masterDoc As String = Request.PhysicalApplicationPath & "PageViews.xml"
Dim writer As XmlTextWriter = Nothing
Dim sb As StringBuilder = Nothing
Dim sw As StringWriter = Nothing
Try
'define variables
sb = New StringBuilder
sw = New StringWriter(sb)
writer = New XmlTextWriter(sw)
'build xml object
writer.Formatting = Formatting.Indented
writer.WriteStartElement("Request", Nothing)
writer.WriteElementString("PageAccessed",
Request.ServerVariables("URL"))
writer.WriteElementString("QueryString",
Request.ServerVariables("QUERY_STRING"))
writer.WriteElementString("IPAddress",
Request.ServerVariables("REMOTE_ADDR"))
writer.WriteElementString("Referer",
Request.ServerVariables("HTTP_REFERER"))
writer.WriteElementString("UserAgent",
Request.ServerVariables("HTTP_USER_AGENT"))
writer.WriteElementString("Date", Date.Now)
writer.WriteEndElement()
writer.Flush()
'make fragment
Dim doc As XmlDocument = New XmlDocument
doc.LoadXml(sb.ToString())
Dim frag As XmlDocumentFragment = doc.CreateDocumentFragment()
Dim node As XmlNode
For Each node In doc.ChildNodes
frag.AppendChild(node)
Next
'add fragment to xml file
doc.Load(masterDoc)
doc.DocumentElement.PrependChild(frag)
doc.Save(masterDoc)
writer.Close()
sw.Close()
Catch ex As Exception
Response.Write("can't do it: " + ex.Message)
Finally
If Not writer Is Nothing Then
writer.Close()
End If
If Not sw Is Nothing Then
sw.Close()
End If
End Try
Thank You
Reddy
Question is
Is it possible to do without using XMLDocument. Because its loading all the
the file into memory. I just want to insert in the front. My code is give
below.
Is it possible to do without using XMLDOcument?
Dim masterDoc As String = Request.PhysicalApplicationPath & "PageViews.xml"
Dim writer As XmlTextWriter = Nothing
Dim sb As StringBuilder = Nothing
Dim sw As StringWriter = Nothing
Try
'define variables
sb = New StringBuilder
sw = New StringWriter(sb)
writer = New XmlTextWriter(sw)
'build xml object
writer.Formatting = Formatting.Indented
writer.WriteStartElement("Request", Nothing)
writer.WriteElementString("PageAccessed",
Request.ServerVariables("URL"))
writer.WriteElementString("QueryString",
Request.ServerVariables("QUERY_STRING"))
writer.WriteElementString("IPAddress",
Request.ServerVariables("REMOTE_ADDR"))
writer.WriteElementString("Referer",
Request.ServerVariables("HTTP_REFERER"))
writer.WriteElementString("UserAgent",
Request.ServerVariables("HTTP_USER_AGENT"))
writer.WriteElementString("Date", Date.Now)
writer.WriteEndElement()
writer.Flush()
'make fragment
Dim doc As XmlDocument = New XmlDocument
doc.LoadXml(sb.ToString())
Dim frag As XmlDocumentFragment = doc.CreateDocumentFragment()
Dim node As XmlNode
For Each node In doc.ChildNodes
frag.AppendChild(node)
Next
'add fragment to xml file
doc.Load(masterDoc)
doc.DocumentElement.PrependChild(frag)
doc.Save(masterDoc)
writer.Close()
sw.Close()
Catch ex As Exception
Response.Write("can't do it: " + ex.Message)
Finally
If Not writer Is Nothing Then
writer.Close()
End If
If Not sw Is Nothing Then
sw.Close()
End If
End Try
Thank You
Reddy