2015-06-19 67 views
3

我想创建带有文本的元素OutputPath。这就是我想要的:创建不带命名空间的XML元素

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> 
    <OutputPath>Text</OutputPath> 
    </PropertyGroup> 

,这就是我得到:

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> 
    <OutputPath xmlns="">Text</OutputPath> 
    </PropertyGroup> 

一切都很好,但是当我创建元素的东西不断增加的xmlns =“”它。

然后我收到错误:错误MSB4097:元素下的元素可能没有自定义的XML名称空间。

// 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's 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; 

创建(新)节点:

// 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; 
+0

Slappy,你使用的是什么delphi版本?最近的delphi版本包括OmniXML支持,并且不公开这种问题。 – whosrdaddy

回答

2

传中,根元素的命名空间创建元素时:

xmlDoc.createElement(PPED^.XmlTag, rootElementNamespace); 

不知根元素的命名空间是什么,但大概你是这样做的。该文档中包含信息,因此我希望您可以这样写:

XmlDoc.DocumentElement.NamespaceURI 

用于根元件名称空间。

我想你的问题应该被认为是一个骗局:How to prevent blank xmlns attributes in output from .NET's XmlDocument?但是我没有关闭它,因为Delphi标记中的mod倾向于不喜欢关闭非Delphi标记的问题。

+0

这是正确的,使用命名空间帮助,谢谢!它看起来像是C#和Delphi中的同一个问题 - IXMLDOMDocument2对象只是一些COM对象的封装 - 对于.NET来说可能是相同的。 – Slappy

+0

确实。这里使用相同的库。 –