如何正确引用 Microsoft.Sdc.Tasks.XmlFile.GetValue 的命名空间

发布于 2024-08-26 12:46:49 字数 1643 浏览 3 评论 0原文

我想使用 MSBuild 将自定义 xml 元素插入到 web.config 中。在网上查找后,我找到了这样的解决方案:

1)将元素存储在projectextensions中的.build文件中

<ProjectExtensions>
 <CustomElement name="CustomElementName">
  ...
 </CustomElement>
</ProjectExtensions>

2)使用GetValue检索元素

<Target name="ModifyConfig">
<XmlFile.GetValue Path="$(MSBuildProjectFullPath)"
               XPath="Project/ProjectExtensions/CustomElement[@name='CustomElementName']">
            <Output TaskParameter="Value" PropertyName="CustomElementProperty"/>
</XmlFile.GetValue>
</Target>

这不起作用,因为我需要引用.build项目的命名空间正在使用它来查找所需的元素(使用 XPath Visualizer 检查 .build 文件)。因此,我寻找进一步的解决方案并得出此结论:

<ItemGroup>
        <XmlNamespace Include="MSBuild">
            <Prefix>msb</Prefix>
            <Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
        </XmlNamespace>
</ItemGroup>

<Target name="ModifyConfig">
<XmlFile.GetValue Path="$(MSBuildProjectFullPath)" Namespaces="$(XmlNamespace)"
               XPath="/msb:Project/msb:ProjectExtensions/msb:CustomElement[@name='CustomElementName']"
                 >
            <Output TaskParameter="Value" PropertyName="CustomElementProperty"/>
</XmlFile.GetValue>
</Target>

但由于某种原因名称空间无法识别 - MSBuild 报告以下错误:

C:...\mybuild.build(53,9): error : A发生任务错误。 C:...\mybuild.build(53,9): 错误:消息 = 命名空间前缀“msb”不是

我尝试了一些以不同方式引用它的变体,但没有任何效果,而且网上也没有太多关于正确引用这些命名空间的信息。你能告诉我我做错了什么以及如何正确地做吗?

i want to use MSBuild to insert a custom xml element into web.config. After looking up online, i found such solution:

1) Store element in the .build file in projectextensions

<ProjectExtensions>
 <CustomElement name="CustomElementName">
  ...
 </CustomElement>
</ProjectExtensions>

2) Retrieve the element with GetValue

<Target name="ModifyConfig">
<XmlFile.GetValue Path="$(MSBuildProjectFullPath)"
               XPath="Project/ProjectExtensions/CustomElement[@name='CustomElementName']">
            <Output TaskParameter="Value" PropertyName="CustomElementProperty"/>
</XmlFile.GetValue>
</Target>

This will not work as i need to reference a namespace the .build project is using for it to find the needed element (checked the .build file with XPath Visualizer). So, i look up for a further solution and come to this:

<ItemGroup>
        <XmlNamespace Include="MSBuild">
            <Prefix>msb</Prefix>
            <Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
        </XmlNamespace>
</ItemGroup>

<Target name="ModifyConfig">
<XmlFile.GetValue Path="$(MSBuildProjectFullPath)" Namespaces="$(XmlNamespace)"
               XPath="/msb:Project/msb:ProjectExtensions/msb:CustomElement[@name='CustomElementName']"
                 >
            <Output TaskParameter="Value" PropertyName="CustomElementProperty"/>
</XmlFile.GetValue>
</Target>

But for some reason namespace is not recognized - MSBuild reports the following error:

C:...\mybuild.build(53,9): error : A task error has occured.
C:...\mybuild.build(53,9): error : Message = Namespace prefix 'msb' is not
defined.

I tried some variations of referencing it differently but none work, and there is not much about propertly referencing those namespaces online also. Can you tell me what am i doing wrong and how to do it propertly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-09-02 12:46:49

我建议使用 MSBuild 社区任务 中名为 XmlMassUpdate 的自定义任务来插入自定义 XML 元素到 xml 文件中。

<XmlMassUpdate 
ContentFile="web.config" 
SubstitutionsFile="changes.xml" 
ContentRoot="/configuration/system.web" 
SubstitutionsRoot="/system.web" /> 

您还可以直接在项目文件中引用 XML,如下所示:

<XmlMassUpdate ContentFile="web.config" ContentRoot="/configuration/system.web"
    NamespaceDefinitions="msb=http://schemas.microsoft.com/developer/msbuild/2003"
    SubstitutionsFile="$(MSBuildProjectFullPath)"
    SubstitutionsRoot="/msb:Project/msb:ProjectExtensions/msb:system.web" />

但是,您的问题的标题似乎表明您在首先获取 XML 值而不更改它们时遇到问题。提到的库还具有 XmlQuery 任务,它从 XML 文件读取值并根据这些值填充参数。

I would recommend using a custom task from MSBuild Community Tasks called XmlMassUpdate for inserting a custom XML element into an xml file.

<XmlMassUpdate 
ContentFile="web.config" 
SubstitutionsFile="changes.xml" 
ContentRoot="/configuration/system.web" 
SubstitutionsRoot="/system.web" /> 

You can also reference the XML directly in the project file like this:

<XmlMassUpdate ContentFile="web.config" ContentRoot="/configuration/system.web"
    NamespaceDefinitions="msb=http://schemas.microsoft.com/developer/msbuild/2003"
    SubstitutionsFile="$(MSBuildProjectFullPath)"
    SubstitutionsRoot="/msb:Project/msb:ProjectExtensions/msb:system.web" />

However, the tile of your question seems to suggest you have problems getting XML values first not changing them. The mentioned lib does also have the XmlQuery task, which reads values from XML files and populates parameters based on those values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文