Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2008

Tip: Looking for answers? Try searching our database.

relative efficiency of methods for accreting xml?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 06 Mar 2008 19:54 GMT
Hi...

I know there's a lot to clean up on how we're doing some of our xml
processing, but I ran into an interesting set of questions.

We have an app that's building an XmlDocument in memory, taking new elements
from various sources.  The new elements are supposed to be tacked onto the
end of the document.

What we're currently doing is (less exception handling):
void AddElement (string newstuff)
{
  XmlDocument newDoc = new XmlDocument();
  newDoc.LoadXml (newstuff);
  oldDoc.AppendChild (oldDoc.ImportNode(newDoc.DocumentElement, true));
}

I was thinking that this might be marginally more efficient:
void AddElement (string newstuff)
{
  oldDoc.InnerXml += newstuff;
}

It avoids creating an extra XmlDocument (and extra name  tables etc) and the
overhead of ImportNode().  The downside (or the good side depending on your
point of view) is that you could now append multiple node fragments at once
plus random text nodes.

I'm sure there are also many other lower level considerations I'm not
thinking of.

What do you think?

Thanks
Mark
Wen Yuan Wang [MSFT] - 07 Mar 2008 08:38 GMT
Hello Mark,

In my opinion, the big difference between InnerXML method and AppendChild
method is that InnerXML method would clear elements/nodes which insides
current node, but AppendChild doesn't.

However, I would like to suggest you may create an XMLDocumentFragment
(instead of XMLDocument) for append method. XMLDocumentFragment is a
lightweight object, very useful for tree insert operations.
For example:

XmlDocument oldDoc = new XmlDocument();
oldDoc.Load("xmlfile1.xml");
XmlDocumentFragment xdf = oldDoc.CreateDocumentFragment();
xdf.InnerXml = "new xml elements..";
oldDoc.DocumentElement.AppendChild(xdf);

This way is easy for reading than innerXML method, and more performance
than XMLDocument solution.

Hope this helps.
Have a great day,
Best regards,

Wen Yuan
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Mark - 07 Mar 2008 15:17 GMT
Hi Wen Yuan...

Thank you for the alternative.  I will give that a try.

Just to clarify, though, it was
oldDoc.DocumentElement.InnerXml += newstring;
so the pre-existing content was preserved and added to
not
oldDoc.DocumentElement.InnerXml = newstring;
which would have gotten rid of whatever was there.

I tested the first one and did see that the original content was preserved.  
Of course it just occurs to me that the downside of the string append is that
the old content would have to be reparsed every time, which would be a big
expense as the document grows.

Your method has the advantage of just parsing the new bits.

Thanks
Mark

> Hello Mark,
>
[quoted text clipped - 45 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Wen Yuan Wang [MSFT] - 10 Mar 2008 09:08 GMT
Hello Mark,
Thanks for your reply.

Just as what you have seen, the XMLDocument parses the whole xml string
which you assigned by innerXML property. Thereby, there is a performance
issue in "oldDoc.DocumentElement.InnerXml += newstring" method when the
document grows. XmlDocumentFragment should be a good solution in such
senario, because it only parses the new element. :)

Best regards,
Wen Yuan

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.