2 simple questions about XmlDocument

StewartBW 1,145 Reputation points
2024-07-11T13:34:25.9433333+00:00

Hello

Dim doc As New XmlDocument

  • Before .Load, using this property has any effect or is default and disregarded?
  1. Before .Load, using this property has any effect or is default and disregarded? doc.PreserveWhitespace = False

Move on:

doc.Load( stream )

  • Now, is it useful or honored if I check for:
If doc.HasChildNodes = True Then
 continue the code...?
End If

I will be using the above for 2 types of xml:

<?xml version="1.0" encoding="utf-8"?>
<categories default="" lastSavedSession="2" lastSavedTime="2023-01-10T21:56:44.535" xmlns="CategoryList.xsd">
  <category usageCount="6" lastSessionUsed="2" name="Purple category" color="8" keyboardShortcut="0" lastTimeUsedNotes="2022-09-13T00:14:40.217" lastTimeUsedJournal="2022-09-13T00:14:40.217" lastTimeUsedContacts="2022-09-13T00:14:40.217" lastTimeUsedTasks="2022-09-13T00:14:40.217" lastTimeUsedCalendar="2022-09-13T00:14:40.217" lastTimeUsedMail="2022-12-07T22:30:38.264" lastTimeUsed="2022-12-07T22:30:38.264" guid="{80ACD1F4-EDB3-4D8E-BC20-AC27185E5851}" />
  <category name="Brown Category" color="2" keyboardShortcut="0" usageCount="0" lastTimeUsedNotes="1601-01-01T00:00:00.000" lastTimeUsedJournal="1601-01-01T00:00:00.000" lastTimeUsedContacts="1601-01-01T00:00:00.000" lastTimeUsedTasks="1601-01-01T00:00:00.000" lastTimeUsedCalendar="1601-01-01T00:00:00.000" lastTimeUsedMail="1601-01-01T00:00:00.000" lastTimeUsed="2023-01-10T21:48:24.358" lastSessionUsed="2" guid="{EBB2BCE6-E5E2-4E36-B836-D8E665379B21}" />
</categories>

And other type:

<?xml version="1.0" encoding="utf-8"?>
<categories elementCount="18">
  <category xml:space="preserve" OPFCategoryCopyBackgroundColor="r:0.57 g:0.48 b:0.82" OPFCategoryCopyName="Birthday"></category>
  <category xml:space="preserve" OPFCategoryCopyBackgroundColor="r:0.45 g:0.60 b:0.88" OPFCategoryCopyName="Blue category"></category>
</categories>

Thanks in advance :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,741 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 55,051 Reputation points
    2024-07-11T13:56:22.3466667+00:00

    I'm not sure I follow what you're asking here.

    using this property

    What property? PreserveWhitespace? For that particular property if you read the docs then it says it determines if the whitespace is preserved on loading and saving so yes it has an impact if set before you call Load. If you don't set it then whitespace is thrown out when the document loads to save space. As such if you then try to write the document back out then all the whitespace has already been stripped out. However there is a difference between significant (which is always preserved and insignificant whitespace which is what the property controls. See here.

    As far as HasChildNodes is concerned it simply indicates whether a node has any children or not. It is slightly faster than going to the nodes themselves and would allow deferring of the load of the nodes. But it probably isn't useful to call it on the document (which inherits it from XmlNode) because a document will have child nodes. An XML document isn't valid unless it has the XML declaration and root element, both of which are children. If Load doesn't throw an exception then the document has children. Use the HasChildNodes property on elements within the document as you're parsing it.


  2. StewartBW 1,145 Reputation points
    2024-07-11T14:38:50.5666667+00:00

    Thanks, yep about HasChildNodes, I wanted to make sure if it has any effect when called on the document itself :)

    0 comments No comments

  3. Jiachen Li-MSFT 32,451 Reputation points Microsoft Vendor
    2024-07-12T06:09:09.1966667+00:00

    Hi @StewartBW ,

    In both cases, the check If doc.HasChildNodes will confirm if there are any child nodes present under the root <categories> element, allowing your code to proceed only when it is safe to do so.

    It's just a check and has no effect on the document itself.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.