【问题标题】:Remove XML comments using Visual Studio 2010 Web Config Transformation使用 Visual Studio 2010 Web Config Transformation 删除 XML 注释
【发布时间】:2011-02-26 20:44:44
【问题描述】:

我们正在使用 Team Build 来处理我们对开发服务器的部署,并且我们需要在转换时从我们的 Web 配置中删除 cmets。有谁知道如何使用转换从 Web 配置文件中删除 <!-- --> 注释行?

【问题讨论】:

    标签: .net visual-studio-2010 web-config transformation


    【解决方案1】:

    我找到了答案。这似乎是 Visual Studio/Team Build 中 XDT 转换引擎中的一个已知错误。这个错误是在三月份报告的,所以不知道什么时候会修复。

    Here's the link

    编辑:此链接实际上与原始问题无关。我们最终认为使用内置的 Web 配置转换是不可能的。所以我们最终编写了一个控制台应用程序来剥离 cmets,并正确格式化转换后的文件。

    【讨论】:

    • 该错误似乎与删除 cmets 无关。
    • 有可能扩展转换,如sedodream.com/2010/09/09/… 中所述。也许,它也可以用于删除 cmets。
    【解决方案2】:

    这是我的功能。您可以将其添加到帮助程序类中:

    public static string RemoveComments(
            string xmlString,
            int indention,
            bool preserveWhiteSpace)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.PreserveWhitespace = preserveWhiteSpace;
            xDoc.LoadXml(xmlString);
            XmlNodeList list = xDoc.SelectNodes("//comment()");
    
            foreach (XmlNode node in list)
            {
                node.ParentNode.RemoveChild(node);
            }
    
            string xml;
            using (StringWriter sw = new StringWriter())
            {
                using (XmlTextWriter xtw = new XmlTextWriter(sw))
                {
                    if (indention > 0)
                    {
                        xtw.IndentChar = ' ';
                        xtw.Indentation = indention;
                        xtw.Formatting = System.Xml.Formatting.Indented;
                    }
    
                    xDoc.WriteContentTo(xtw);
                    xtw.Close();
                    sw.Close();
                }
                xml = sw.ToString();
            }
    
            return xml;
        }
    

    【讨论】:

      【解决方案3】:

      如果您有小部分想要从中删除 cmets,您可能愿意使用替换转换。

      基础 web.config 文件:

      <system.webServer>
          <rewrite>
              <rules>
                  <clear />
                  <!-- See transforming configs to see values inserted for builds -->
              </rules>
          </rewrite>
      

      web.release.config 转换(替换没有评论的内容):

      <system.webServer>
      <rewrite >
        <rules xdt:Transform="Replace">
          <clear/>
          <rule name="Redirect to https" stopProcessing="true" >
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
          </rule>
        </rules>
      </rewrite>
      

      最终发布的配置结果:

      <system.webServer>
      <rewrite>
        <rules>
          <clear />
          <rule name="Redirect to https" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
          </rule>
        </rules>
      </rewrite>
      

      您最终可能会使用这种方法将大量配置从基础复制到转换文件,但它可能适用于小情况...

      在我的情况下,我不想在我的基础中重写规则,但我发表评论告诉其他开发人员查看转换以获取更多信息,但我不希望在最终版本中发表评论。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        • 1970-01-01
        • 2011-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多