[Mono-list] Questions about writing an XML document

Andreas Färber andreas.faerber at web.de
Tue May 9 15:40:15 EDT 2006


Hi Loren,

>> Loren Bandiera schrieb:
>>     
>>> To create it, I've tried doing something like this:
>>>
>>> XmlDocument xmlDoc = new XmlDocument ();
>>> xmlDoc.AppendChild (xmlDoc.CreateNode
>>> (XmlNodeType.XmlDeclaration,"",""));
>>>
>>> XmlElement docElement = xmlDoc.CreateElement ("doc");
>>> docElement.SetAttribute ("xmlns", "http://whatever.org/XMLSchema/foo");
>>>
>>>       
>> I believe CreateElement is actually overloaded so that you can give the
>> namespace directly as an argument without setting it manually
>> afterwards. :-)
>>
>> HTH,
>>
>> Andreas
>>     
>
> You are correct you can do that with my original example. However the
> actual document I'm working with has more than one namespace I need to
> specify. For example:
>
> <oval_system_characteristics
>   xmlns="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5"
>   xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5"
>   xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5"
>   xmlns:oval-sc="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5"
>   xmlns:unix-sc="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#unix"
>   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>   xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5
> oval-definitions-schema.xsd
>     http://oval.mitre.org/XMLSchema/oval-system-characteristics-5
> oval-system-characteristics-schema.xsd
>     http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#unix
> unix-system-characteristics-schema.xsd
>     http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd">
>
> ...
>
> </oval_system_characteristics>
>
> XmlDocument.CreateElement() only lets you specify one (at far as I am
> aware) so that is why I was using .SetAttribute to do it.
>
>   
I understood that, I do the same to have the wsse namespace definition
within the SOAP envelope. :-)

CreateElement only lets you specify one because XML-Namespaces only
allows the association of an element with one namespace and the DOM has
no provision for associating attributes during element creation.

Still instead of CreateElement("nameonly"); SetAttribute("xmlns",
"something"); write CreateElement("something", "nameonly"); - my point
was independent of any subsequent SetAttribute calls for the additional
namespaces. It's just like writing int x = 3; x = 4;, which should be
replaced by int x = 4;. ;-) The latter as a local variable might be
optimized but the XML creation most likely not...

Andreas


More information about the Mono-list mailing list