创建没有名称空间的XML元素

时间:2015-06-19 18:33:58

标签: xml delphi

我想用文本创建元素 OutputPath 。这就是我想要的:

  <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;

1 个答案:

答案 0 :(得分:2)

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

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

我不知道root元素的命名空间是什么,但可能是你做的。该文档也有信息,所以我希望你能写下:

XmlDoc.DocumentElement.NamespaceURI

表示根元素名称空间。

我想你的问题应该被认为是这个问题:How to prevent blank xmlns attributes in output from .NET's XmlDocument?但是我并没有这样做,因为Delphi标签中的mod往往不喜欢关闭来自非Delphi的问题标签

相关问题