xml - 创建不带命名空间的 XML 元素

标签 xml delphi

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

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

这就是我得到的:

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

一切都很好,但是当我创建元素时,某些东西不断向它添加 xmlns=""

然后我收到错误:error MSB4097: The element below element may not have a custom 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'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;

最佳答案

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

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

我不知道根元素的 namespace 是什么,但您可能知道。该文件也有信息,所以我希望你可以写:

XmlDoc.DocumentElement.NamespaceURI

用于根元素命名空间。

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

关于xml - 创建不带命名空间的 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945049/

相关文章:

delphi - 将自定义控件添加到另一个自定义控件

php - 使用 php 和 mysql 插入 xml

PHP XML Expat 解析器 : how to read only part of the XML document?

java - 使用 JAXB 将多个 XML 元素的内容提取为文本

java - 如何创建采用 Generic 对象的 JAXB Marshaller

delphi - 将 C stdcall 接口(interface)方法中的变量参数转换为 Delphi

java - 如何使 imageView 在屏幕的一侧无限移动到另一侧? [安卓动画]

delphi 5 应用程序在 win7 x64 的面板上显示错误

delphi - 如何将多个不同的记录(由于delphi限制而不是类)传递给一个函数?

C# 等效于 Delphi 的 .INI 读取方法