By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,719 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,719 IT Pros & Developers. It's quick & easy.

add Node into XML file using XMLTextWriter without using XMLDocume

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
No. Think about what the system would have to do to insert content into the
middle of some text. First it has to locate the position (the front is
relatively easy to locate). It then has to push the existing content out of
the way. This requires the allocation of more file space and the moving of
each character on position to the right. Finally it inserts what you need.
Essentially it would have to load the entire document in memory just to
shuffle around the characters (even without the DOM) by using parse methods.
The text writers are forward only and do not interact with what is already
in the stream, meaning that it would probably overwrite the contents that
are already there.
"reddy" <re***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
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

Jul 21 '05 #2

P: n/a
Thank you for the reply Peter. So you are saying I have to use XMLDocument.
Actually i want to keep that code in global.asax file to track the requests
to our webserver.

Do you think is this going to effect the performance because of this DOM?
Right now i am testing. It's working fine. But once i keep it in production
i fear about the performance issues.
"Peter Rilling" wrote:
No. Think about what the system would have to do to insert content into the
middle of some text. First it has to locate the position (the front is
relatively easy to locate). It then has to push the existing content out of
the way. This requires the allocation of more file space and the moving of
each character on position to the right. Finally it inserts what you need.
Essentially it would have to load the entire document in memory just to
shuffle around the characters (even without the DOM) by using parse methods.
The text writers are forward only and do not interact with what is already
in the stream, meaning that it would probably overwrite the contents that
are already there.
"reddy" <re***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
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


Jul 21 '05 #3

P: n/a
I think it'd be better performance-wise to create the xml as a plain string,
and then try to append it at the end of the document without too many string
operations ( for finding the end tag of the xml for example ... )

Fro instance, if you know the exact size of your end-line in bytes, you
could just open the file as a strem, move the file pointer just before the
end-line, and write your xml (string) content there very fast ...

You see, loading a doc into an XmlDocument not only opens & reads the file
into memory, but also performs the full parsing & object(s) creation
required for DOM objects ... which could be a serious performance killer in
an xml file with a few thousand elements.

Angel
O:]
"reddy" <re***@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Thank you for the reply Peter. So you are saying I have to use XMLDocument. Actually i want to keep that code in global.asax file to track the requests to our webserver.

Do you think is this going to effect the performance because of this DOM?
Right now i am testing. It's working fine. But once i keep it in production i fear about the performance issues.
"Peter Rilling" wrote:
No. Think about what the system would have to do to insert content into the middle of some text. First it has to locate the position (the front is
relatively easy to locate). It then has to push the existing content out of the way. This requires the allocation of more file space and the moving of each character on position to the right. Finally it inserts what you need. Essentially it would have to load the entire document in memory just to
shuffle around the characters (even without the DOM) by using parse methods. The text writers are forward only and do not interact with what is already in the stream, meaning that it would probably overwrite the contents that are already there.
"reddy" <re***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
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


Jul 21 '05 #4

P: n/a
Thank You Angelos.
Your idea of using plain string sounds good. But I need to insert child
node right, so I am not exactly appending. Kind of inserting that node
infornt of end of root node.
I didn't quite get ur idea of "the exact size of your end-line in bytes".
Could you please explain a little bit more.

Thank you very much
Reddy

"Angelos Karantzalis" wrote:
I think it'd be better performance-wise to create the xml as a plain string,
and then try to append it at the end of the document without too many string
operations ( for finding the end tag of the xml for example ... )

Fro instance, if you know the exact size of your end-line in bytes, you
could just open the file as a strem, move the file pointer just before the
end-line, and write your xml (string) content there very fast ...

You see, loading a doc into an XmlDocument not only opens & reads the file
into memory, but also performs the full parsing & object(s) creation
required for DOM objects ... which could be a serious performance killer in
an xml file with a few thousand elements.

Angel
O:]
"reddy" <re***@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Thank you for the reply Peter. So you are saying I have to use

XMLDocument.
Actually i want to keep that code in global.asax file to track the

requests
to our webserver.

Do you think is this going to effect the performance because of this DOM?
Right now i am testing. It's working fine. But once i keep it in

production
i fear about the performance issues.
"Peter Rilling" wrote:
No. Think about what the system would have to do to insert content into the middle of some text. First it has to locate the position (the front is
relatively easy to locate). It then has to push the existing content out of the way. This requires the allocation of more file space and the moving of each character on position to the right. Finally it inserts what you need. Essentially it would have to load the entire document in memory just to
shuffle around the characters (even without the DOM) by using parse methods. The text writers are forward only and do not interact with what is already in the stream, meaning that it would probably overwrite the contents that are already there.
"reddy" <re***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
> 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


Jul 21 '05 #5

P: n/a
Say your xml starts with "<log>" and that's where you want to insert the new
data.

You've two options:
1) Work with strings, i.e. load the file as a string in memory and
"play-around" with that.
2) Use streams to make things a little faster.

In the first case, you know that you want to insert 5 characters after the
start of the document, so things are pretty straight-forward. cut the string
in two, insert the new xml where it's supposed to be, join the strings again
& save to disk.

In the second case, you pretty much need to do the same operations, only
you're working with streams - bytes in other words. So, you need to know how
many bytes from the start of the document you want to insert the new xml in
as a byte[] ... so, you need to "translate" the length of your first ( I
though it was the last in my previous post ) line from characters to bytes.

The second (stream-based) way is actually a bit more complex from a coding
perspective, but, if implemented properly, it will cut-down much on memory
usage & processing speed. You won't have to parse the whole document in
memory. But it will give you some trouble because you'll have to read the
complete stream byte-by-byte and write it out another stream adding the
extra content (possibly in a new 'temp' file that you'll have to swap with
the original after all the reading/writing's completed )

That's it, i think :) Have fun coding !!!

Angel
O:]
"reddy" <re***@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
Thank You Angelos.
Your idea of using plain string sounds good. But I need to insert child
node right, so I am not exactly appending. Kind of inserting that node
infornt of end of root node.
I didn't quite get ur idea of "the exact size of your end-line in bytes".
Could you please explain a little bit more.

Thank you very much
Reddy

"Angelos Karantzalis" wrote:
I think it'd be better performance-wise to create the xml as a plain string, and then try to append it at the end of the document without too many string operations ( for finding the end tag of the xml for example ... )

Fro instance, if you know the exact size of your end-line in bytes, you
could just open the file as a strem, move the file pointer just before the end-line, and write your xml (string) content there very fast ...

You see, loading a doc into an XmlDocument not only opens & reads the file into memory, but also performs the full parsing & object(s) creation
required for DOM objects ... which could be a serious performance killer in an xml file with a few thousand elements.

Angel
O:]
"reddy" <re***@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Thank you for the reply Peter. So you are saying I have to use

XMLDocument.
Actually i want to keep that code in global.asax file to track the

requests
to our webserver.

Do you think is this going to effect the performance because of this DOM? Right now i am testing. It's working fine. But once i keep it in

production
i fear about the performance issues.
"Peter Rilling" wrote:

> No. Think about what the system would have to do to insert content into
the
> middle of some text. First it has to locate the position (the front
is > relatively easy to locate). It then has to push the existing content out of
> the way. This requires the allocation of more file space and the
moving of
> each character on position to the right. Finally it inserts what
you need.
> Essentially it would have to load the entire document in memory just
to > shuffle around the characters (even without the DOM) by using parse

methods.
> The text writers are forward only and do not interact with what is

already
> in the stream, meaning that it would probably overwrite the contents

that
> are already there.
>
>
> "reddy" <re***@discussions.microsoft.com> wrote in message
> news:63**********************************@microsof t.com...
> > 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
>
>
>


Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.