代码之家  ›  专栏  ›  技术社区  ›  Dylan Vester

使用Visual Studio 2010 Web配置转换删除XML注释

  •  5
  • Dylan Vester  · 技术社区  · 16 年前

    <!-- --> 使用转换从web配置文件中删除注释行?

    3 回复  |  直到 15 年前
        1
  •  3
  •   Dylan Vester    15 年前

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

    Here's the link

    编辑: 这个链接实际上与原来的问题无关。我们最终发现,使用内置的web配置转换是不可能的。因此,我们最终编写了一个控制台应用程序来剥离注释,并正确格式化转换后的文件。

        2
  •  2
  •   David.Chu.ca    15 年前

    这是我的职责。可以将其添加到助手类:

    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
  •  1
  •   Ian Robertson    9 年前

    基本web.config文件:

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

    web.release.config transfrom(替换无注释的内容):

    <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>
    

    使用这种方法,您可能会将大量配置从基本文件复制到转换文件,但在小情况下可能会比较合适。。。

    在我的例子中,我不想在我的基础中重写规则,但我在其中添加了一条注释,告诉其他开发人员在转换中查找更多信息,但我不希望在最终版本中使用该注释。