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
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.