473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

add Node into XML file using XMLTextWriter without using XMLDocume

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
5 6113
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Magnus | last post by:
can anyone help me on how to create and manipulate a xmttextwriter without having to craete a physical file. I have an application that should return data in xml. But I do not want to create a...
6
by: Nad | last post by:
Hello, I need some help with this: I am trying to update some data using XmlTextWriter. I instantiate first and then do some validation and then copy data into the dataset. Finally I commit...
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
3
by: Atara | last post by:
I use the following code to load xml file: Dim srcXml As String = pathDataFiles & "test.xml" Dim XmlDoc As New Xml.XmlDocument XmlDoc.Load(srcXml) If my xml file starts with the line - ...
7
by: reddy | last post by:
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...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
1
by: deepika patra | last post by:
hi all, i want to modify the value of a node of a xml file. i have to do this in c#.net language. i have to assume that the node value of the file wich i want to modify is already created.i...
0
by: jazzygirl | last post by:
I'm fairly new to vb.net (self-taught and realizing how much I DON'T know). I need to create an XML output file (I'm quite ignorant in this area as well!). I used an XMLDataDocument and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.