使用 nant 将属性添加到 xml 节点

发布于 2024-07-11 07:09:10 字数 89 浏览 8 评论 0原文

有没有办法使用 nant 将属性添加到 xml 节点(我有其 xpath)? 尝试了 xmlpoke,但看起来它只能更新现有属性。

谢谢。

Is there a way to add an attribute to an xml node (which I have the xpath of) using nant?
Tried xmlpoke but it looks like it can only update existing attributes.

thanks.

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

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

发布评论

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

评论(2

老娘不死你永远是小三 2024-07-18 07:09:10

XmlPoke 肯定不起作用,因为 xpath 必须首先匹配某些内容才能替换它。

我知道执行此操作的唯一方法是创建您自己的任务,该任务允许您将数据添加到 xml 文件。 这些新任务可以单独构建并通过将 dll 复制到 NAnt\bin 文件夹中或直接从构建文件扩展 NAnt 来添加到 NAnt

。 sourceforge.net/release/0.85/help/tasks/script.html" rel="nofollow noreferrer">

如果您碰巧使此任务足够通用,那么尝试将其提交到 NAntContrib< 可能会很好/a> 所以每个人都受益。

XmlPoke will definitely not work because the xpath must match something in the first place to be able to replace it.

The only way I know of doing this is to create your own task that would allow you to add data to an xml file. These new tasks can either be build separately and added to NAnt by copying dlls into NAnt\bin folder, or by extending NAnt directly from your build files

The information to get you started is found on <script/> Task

If you happen to make this task generic enough, it might be good to try to submit it to NAntContrib so everyone benefits.

↙厌世 2024-07-18 07:09:10

我最近做了类似的东西。 这是用于插入节点,但应该很容易更改。

<script language="C#" prefix="test" >
        <references>
            <include name="System.Xml.dll" />
        </references>
        <code>
            <![CDATA[
              [TaskName("xmlinsertnode")]
              public class TestTask : Task {
                #region Private Instance Fields
                private string _filename;
                private string _xpath;
                private string _fragment;
                #endregion Private Instance Fields

                #region Public Instance Properties
                [TaskAttribute("filename", Required=true)]
                public string FileName {
                    get { return _filename; }
                    set { _filename = value; }
                }

                [TaskAttribute("xpath", Required=true)]
                public string XPath {
                    get { return _xpath; }
                    set { _xpath = value; }
                }

                [TaskAttribute("fragment", Required=true)]
                public string Fragment {
                    get { return _fragment; }
                    set { _fragment = value; }
                }

                #endregion Public Instance Properties

                #region Override implementation of Task
                protected override void ExecuteTask() {
                    System.Xml.XmlDocument document = new System.Xml.XmlDocument();
                    document.Load(_filename);
                    System.Xml.XPath.XPathNavigator navigator = document.CreateNavigator();
                    navigator.SelectSingleNode(_xpath).AppendChild(_fragment);
                    document.Save(_filename);
                }
                #endregion Override implementation of Task
              }
            ]]>
        </code>
    </script>

I made something similar recently. This is for inserting nodes, but should be easily changed.

<script language="C#" prefix="test" >
        <references>
            <include name="System.Xml.dll" />
        </references>
        <code>
            <![CDATA[
              [TaskName("xmlinsertnode")]
              public class TestTask : Task {
                #region Private Instance Fields
                private string _filename;
                private string _xpath;
                private string _fragment;
                #endregion Private Instance Fields

                #region Public Instance Properties
                [TaskAttribute("filename", Required=true)]
                public string FileName {
                    get { return _filename; }
                    set { _filename = value; }
                }

                [TaskAttribute("xpath", Required=true)]
                public string XPath {
                    get { return _xpath; }
                    set { _xpath = value; }
                }

                [TaskAttribute("fragment", Required=true)]
                public string Fragment {
                    get { return _fragment; }
                    set { _fragment = value; }
                }

                #endregion Public Instance Properties

                #region Override implementation of Task
                protected override void ExecuteTask() {
                    System.Xml.XmlDocument document = new System.Xml.XmlDocument();
                    document.Load(_filename);
                    System.Xml.XPath.XPathNavigator navigator = document.CreateNavigator();
                    navigator.SelectSingleNode(_xpath).AppendChild(_fragment);
                    document.Save(_filename);
                }
                #endregion Override implementation of Task
              }
            ]]>
        </code>
    </script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文