使用 Inno Setup 更改配置标记中包含命名空间的 web.config [duplicate]

Changing web.config containing namespace in configuration tag with Inno Setup [duplicate]

提问人:Markku Rintala 提问时间:9/27/2023 最后编辑:Markku Rintala 更新时间:9/29/2023 访问量:70

问:

我想在web.config文件中为节点设置一个新属性,其中包含使用Inno Setup创建的安装包。 为了设置此属性,我使用过程 SaveAttributeValueToXML

我的web.config文件以带有命名空间的配置部分开头。我正在尝试向标签添加一个属性

web.config 文件结构如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
  <system.web.extensions>
    <scripting>
      <webServices>
        <authenticationService enabled="true"/>
        <roleService enabled="true"/>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

我想向authenticationService添加一个新属性,如下所示:

   <authenticationService enabled="true" requireSSL = "true"/>

当我调用 SaveAttributeValueToXML 时

SaveAttributeValueToXML(ConfigFileName, '/configuration/system.web.extensions/scripting/webServices/authenticationService', 'requireSSL', 'True');

由于配置标记中的命名空间,该过程找不到节点,并在

XMLNode.setAttribute(AAttribute, AValue);

当我从 web.config -file 中删除命名空间定义时,上述过程调用起作用了。

对于 C#,似乎有 XmlNamespaceManager,它可以处理 xml 文档中的 xml 命名空间。对于 Inno Setup,根据 Inno Setup - XML 编辑 XPath 请求失败并显示“运行时出现 NIL 接口异常”,我可以使用

XMLDocument.setProperty('SelectionNamespaces', xmlns:ns=''http://_namespace_''');.

如果我在 SaveAttributeValueToXML 中更改

  XMLDocument.setProperty('SelectionLanguage', 'XPath');
  XMLDocument.setProperty('SelectionNamespaces', 'xmlns:ns=''http://schemas.microsoft.com/.NetConfiguration/v2.0''');
  XMLNode := XMLDocument.selectSingleNode('//ns:configuration/system.web.extensions/scripting/webServices/authenticationService');
  XMLNode.setAttribute(AAttribute, AValue);

XMLNode.setAttribute 仍将创建异常“Variant is null, cannot invoke”。因此,selectSingleNode 不会返回任何内容。我一定错过了什么。

web-config inno-setup xml-命名空间

评论


答:

0赞 Markku Rintala 9/29/2023 #1

感谢 Martin Prikryls 的评论和提示,我为我的问题创建了这个解决方案。

我在过程 SaveAttributeValueToXML 中为 Namespace 添加了一个新参数。

procedure SaveAttributeValueToXML(const AFileName, NameSpace, APath, AAttribute, AValue: string);
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('The XML file could not be parsed. ' + 
        XMLDocument.parseError.reason, mbError, MB_OK)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      if Length(NameSpace) > 0 then
        XMLDocument.setProperty('SelectionNamespaces', 'xmlns:ns=''' + NameSpace + '''');
      XMLNode := XMLDocument.selectSingleNode(APath);
      XMLNode.setAttribute(AAttribute, AValue);
      XMLDocument.save(AFileName);
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, 
      mbError, MB_OK); 
    Log('SaveAttributeValueToXML, AFileName: ' + AFileName + 
      ', APath: ' + APath +
      ', AAttribute: ' + AAttribute +
      ', AValue: ' + AValue); 
  end;
end;

就我而言,我称它为这样的

SaveAttributeValueToXML(ConfigFileName, 'http://schemas.microsoft.com/.NetConfiguration/v2.0', '//ns:configuration/ns:system.web.extensions/ns:scripting/ns:webServices/ns:authenticationService', 'requireSSL', 'True');