Creating an XML element without a namespace
I want to create an OutputPath element with text. This is what I want:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>Text</OutputPath>
</PropertyGroup>
and this is what I get:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath xmlns="">Text</OutputPath>
</PropertyGroup>
Everything is fine, but when I create the element, something keeps adding xmlns = "" .
And then I get the error: Error MSB4097: The element under the element may not have its own XML namespace.
// Load the Project (.innoproj or .nsisproj file) xmlDoc := nil; currentConfigurationNode := nil; xmlDoc := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument2; xmlDoc.async := False; //xmlDoc.setProperty('SelectionNamespaces', 'xmlns="http://schemas.microsoft.com/developer/msbuild/2003"'); << this line does nothing xmlDoc.load(projPath); if xmlDoc.parseError.errorCode <> 0 then begin xmlDoc := nil; currentConfigurationNode := nil; raise Exception.Create('Cannot not load Project file! Details: ' + xmlDoc.parseError.reason); end; // Find appropriate element and get it value { <?xml><Project><PropertyGroup Condition=" '$(Configuration)' == 'XXX' "> } propertyGroup := xmlDoc.selectNodes('/Project/PropertyGroup'); for I := 0 to propertyGroup.length - 1 do begin node := propertyGroup[I]; if (node.attributes.length > 0) then begin Temp := String(node.attributes.getNamedItem('Condition').Text); if(Temp.Contains('$(Configuration)') and Temp.Contains(projConfiguration)) then begin // Do all operations on this node currentConfigurationNode := propertyGroup[I]; break; end; end; end; Result := True; except on Exception do Result := False; end;
Creating a (new) node:
// This is correct Node for selected Configuration node := currentConfigurationNode.selectSingleNode(PPED^.XmlTag); if(node <> nil) then if(PPED^.Value <> '') then begin elementNode := currentConfigurationNode.appendChild(xmlDoc.createElement(PPED^.XmlTag)); elementNode.text := PutSymbol(PPED^.Strip, PPED^.Value); << Something adds xmls="" to this element end;
source to share
Go to the namespace of the root elements when creating the element:
xmlDoc.createElement(PPED^.XmlTag, rootElementNamespace);
I don't know what the root element namespace is, but presumably you do. There is information in the doc, so I expect you to be able to write:
XmlDoc.DocumentElement.NamespaceURI
for the namespace of the root elements.
I guess your question should be considered a trick of this: How to prevent empty xmlns attributes from appearing in the output from XmlDocument.NET? But I didn't notice t close it as such, because mods in a Delphi tag tend to dislike closing question answers from non-Delphi tags.
source to share