Quantcast

Multiple top-level nodes with XmlDocument

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Multiple top-level nodes with XmlDocument

Tobias Boege-2
Hi,

I want to write an XML file like this:

  <?xml version="1.0" encoding="utf-8" ?>
  <!-- Introductory comment -->
  <tag />

Note that I have a comment node and an element node at the top-level of the
DOM tree. This [1] gives me the impression that it should be valid XML.

This is straight forward with XmlWriter, but it doesn't seem possible with
XmlDocument since it allows for only one Root node. XmlDocument.Root is an
XmlElement and consequently always renders as

  <RootName>
    ...
  </RootName>

which can even produce

  <>
  </>

in the output XML file if you set XmlDocument.Root.Name = Null.

Regards,
Tobi

[1] http://stackoverflow.com/questions/14726276/what-is-the-root-node-in-xml

--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple top-level nodes with XmlDocument

Adrien Prokopowicz-2
Le Sat, 08 Apr 2017 17:23:32 +0200, Tobias Boege <[hidden email]> a  
écrit:

> Hi,
>
> I want to write an XML file like this:
>
>   <?xml version="1.0" encoding="utf-8" ?>
>   <!-- Introductory comment -->
>   <tag />
>
> Note that I have a comment node and an element node at the top-level of  
> the
> DOM tree. This [1] gives me the impression that it should be valid XML.
>
> This is straight forward with XmlWriter, but it doesn't seem possible  
> with
> XmlDocument since it allows for only one Root node. XmlDocument.Root is  
> an
> XmlElement and consequently always renders as
>
>   <RootName>
>     ...
>   </RootName>
>
> which can even produce
>
>   <>
>   </>
>
> in the output XML file if you set XmlDocument.Root.Name = Null.
>
> Regards,
> Tobi
>
> [1]  
> http://stackoverflow.com/questions/14726276/what-is-the-root-node-in-xml
>

Hi Tobias,

A document is technically a node itself, and can have multiple children.
The "Root" property is only defined to be the direct element child of
the document (a document cannot have multiple root elements).

This is how the XmlDocument is represented internally, but the exposed
Gambas class does not inherit XmlNode. I think it is for compatibility
reasons with the old gb.xml, but I have to re-check.

In the meantime, you can work around this limitation like this :

   Dim doc As New XmlDocument

   doc.FromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?><!--  
Introductory comment --><tag />")

   doc.Root.TextContent = "Hello world"

   Print doc.ToString() 'Correctly renders the comments

Regards,

--
Adrien Prokopowicz

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Loading...