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 | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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" <reddy@discussions.microsoft.com> wrote in message
news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.com...[color=blue]
> 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[/color]
the[color=blue]
> 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 &[/color]
"PageViews.xml"[color=blue]
> 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[/color] | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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" <reddy@discussions.microsoft.com> wrote in message
news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.com...[color=blue]
> 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[/color]
the[color=blue]
> 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 &[/color]
"PageViews.xml"[color=blue]
> 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[/color] | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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:
[color=blue]
> 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" <reddy@discussions.microsoft.com> wrote in message
> news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.com...[color=green]
> > 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[/color]
> the[color=green]
> > 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 &[/color]
> "PageViews.xml"[color=green]
> > 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[/color]
>
>
>[/color] | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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:
[color=blue]
> 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" <reddy@discussions.microsoft.com> wrote in message
> news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.com...[color=green]
> > 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[/color]
> the[color=green]
> > 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 &[/color]
> "PageViews.xml"[color=green]
> > 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[/color]
>
>
>[/color] | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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" <reddy@discussions.microsoft.com> wrote in message
news:9545F56E-F795-4567-8313-3A80E59B986E@microsoft.com...[color=blue]
> Thank you for the reply Peter. So you are saying I have to use[/color]
XMLDocument.[color=blue]
> Actually i want to keep that code in global.asax file to track the[/color]
requests[color=blue]
> 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[/color]
production[color=blue]
> i fear about the performance issues.
>
>
> "Peter Rilling" wrote:
>[color=green]
> > No. Think about what the system would have to do to insert content into[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
out of[color=blue][color=green]
> > the way. This requires the allocation of more file space and the moving[/color][/color]
of[color=blue][color=green]
> > each character on position to the right. Finally it inserts what you[/color][/color]
need.[color=blue][color=green]
> > Essentially it would have to load the entire document in memory just to
> > shuffle around the characters (even without the DOM) by using parse[/color][/color]
methods.[color=blue][color=green]
> > The text writers are forward only and do not interact with what is[/color][/color]
already[color=blue][color=green]
> > in the stream, meaning that it would probably overwrite the contents[/color][/color]
that[color=blue][color=green]
> > are already there.
> >
> >
> > "reddy" <reddy@discussions.microsoft.com> wrote in message
> > news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.com...[color=darkred]
> > > 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[/color][/color][/color]
all[color=blue][color=green]
> > the[color=darkred]
> > > the file into memory. I just want to insert in the front. My code is[/color][/color][/color]
give[color=blue][color=green][color=darkred]
> > > below.
> > > Is it possible to do without using XMLDOcument?
> > >
> > >
> > > Dim masterDoc As String = Request.PhysicalApplicationPath &[/color]
> > "PageViews.xml"[color=darkred]
> > > 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 =[/color][/color][/color]
doc.CreateDocumentFragment()[color=blue][color=green][color=darkred]
> > > 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[/color]
> >
> >
> >[/color][/color] | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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:
[color=blue]
> 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" <reddy@discussions.microsoft.com> wrote in message
> news:9545F56E-F795-4567-8313-3A80E59B986E@microsoft.com...[color=green]
> > Thank you for the reply Peter. So you are saying I have to use[/color]
> XMLDocument.[color=green]
> > Actually i want to keep that code in global.asax file to track the[/color]
> requests[color=green]
> > 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[/color]
> production[color=green]
> > i fear about the performance issues.
> >
> >
> > "Peter Rilling" wrote:
> >[color=darkred]
> > > No. Think about what the system would have to do to insert content into[/color][/color]
> the[color=green][color=darkred]
> > > 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[/color][/color]
> out of[color=green][color=darkred]
> > > the way. This requires the allocation of more file space and the moving[/color][/color]
> of[color=green][color=darkred]
> > > each character on position to the right. Finally it inserts what you[/color][/color]
> need.[color=green][color=darkred]
> > > Essentially it would have to load the entire document in memory just to
> > > shuffle around the characters (even without the DOM) by using parse[/color][/color]
> methods.[color=green][color=darkred]
> > > The text writers are forward only and do not interact with what is[/color][/color]
> already[color=green][color=darkred]
> > > in the stream, meaning that it would probably overwrite the contents[/color][/color]
> that[color=green][color=darkred]
> > > are already there.
> > >
> > >
> > > "reddy" <reddy@discussions.microsoft.com> wrote in message
> > > news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.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[/color][/color]
> all[color=green][color=darkred]
> > > the
> > > > the file into memory. I just want to insert in the front. My code is[/color][/color]
> give[color=green][color=darkred]
> > > > 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 =[/color][/color]
> doc.CreateDocumentFragment()[color=green][color=darkred]
> > > > 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
> > >
> > >
> > >[/color][/color]
>
>
>[/color] | | | | re: add Node into XML file using XMLTextWriter without using XMLDocume
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" <reddy@discussions.microsoft.com> wrote in message
news:89FCC753-75D5-4B27-A97E-633928BE6E31@microsoft.com...[color=blue]
> 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:
>[color=green]
> > I think it'd be better performance-wise to create the xml as a plain[/color][/color]
string,[color=blue][color=green]
> > and then try to append it at the end of the document without too many[/color][/color]
string[color=blue][color=green]
> > 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[/color][/color]
the[color=blue][color=green]
> > end-line, and write your xml (string) content there very fast ...
> >
> > You see, loading a doc into an XmlDocument not only opens & reads the[/color][/color]
file[color=blue][color=green]
> > into memory, but also performs the full parsing & object(s) creation
> > required for DOM objects ... which could be a serious performance killer[/color][/color]
in[color=blue][color=green]
> > an xml file with a few thousand elements.
> >
> > Angel
> > O:]
> >
> >
> > "reddy" <reddy@discussions.microsoft.com> wrote in message
> > news:9545F56E-F795-4567-8313-3A80E59B986E@microsoft.com...[color=darkred]
> > > Thank you for the reply Peter. So you are saying I have to use[/color]
> > XMLDocument.[color=darkred]
> > > Actually i want to keep that code in global.asax file to track the[/color]
> > requests[color=darkred]
> > > to our webserver.
> > >
> > > Do you think is this going to effect the performance because of this[/color][/color][/color]
DOM?[color=blue][color=green][color=darkred]
> > > Right now i am testing. It's working fine. But once i keep it in[/color]
> > production[color=darkred]
> > > i fear about the performance issues.
> > >
> > >
> > > "Peter Rilling" wrote:
> > >
> > > > No. Think about what the system would have to do to insert content[/color][/color][/color]
into[color=blue][color=green]
> > the[color=darkred]
> > > > middle of some text. First it has to locate the position (the front[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > > relatively easy to locate). It then has to push the existing[/color][/color][/color]
content[color=blue][color=green]
> > out of[color=darkred]
> > > > the way. This requires the allocation of more file space and the[/color][/color][/color]
moving[color=blue][color=green]
> > of[color=darkred]
> > > > each character on position to the right. Finally it inserts what[/color][/color][/color]
you[color=blue][color=green]
> > need.[color=darkred]
> > > > Essentially it would have to load the entire document in memory just[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > shuffle around the characters (even without the DOM) by using parse[/color]
> > methods.[color=darkred]
> > > > The text writers are forward only and do not interact with what is[/color]
> > already[color=darkred]
> > > > in the stream, meaning that it would probably overwrite the contents[/color]
> > that[color=darkred]
> > > > are already there.
> > > >
> > > >
> > > > "reddy" <reddy@discussions.microsoft.com> wrote in message
> > > > news:6369AE12-AAD1-45BF-8434-98A9D3CF8BE3@microsoft.com...
> > > > > I am trying to insert a node into an XMLFile. using XMLTextwriter.[/color][/color][/color]
My[color=blue][color=green][color=darkred]
> > > > > Question is
> > > > > Is it possible to do without using XMLDocument. Because its[/color][/color][/color]
loading[color=blue][color=green]
> > all[color=darkred]
> > > > the
> > > > > the file into memory. I just want to insert in the front. My code[/color][/color][/color]
is[color=blue][color=green]
> > give[color=darkred]
> > > > > 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 =[/color]
> > doc.CreateDocumentFragment()[color=darkred]
> > > > > 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
> > > >
> > > >
> > > >[/color]
> >
> >
> >[/color][/color] |  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|