0

我正在使用 msbuild 进行一些自动化。其中一项任务是 sql 查询以获取表的 xml 表示并将其写入文件。所以我正在使用

<MSBuild.ExtensionPack.SqlServer.SqlExecute 
    ConnectionString="$(AdminConnectionString)" 

    Sql="SELECT '%(ReaderResult.Identity)' as XmlFileName, 
     (SELECT * FROM %(ReaderResult.Identity) FOR XML AUTO, TYPE, ELEMENTS,
      XMLSCHEMA('%(ReaderResult.Identity)'), ROOT('DataSet'))  as FileContent"
    ContinueOnError="false" TaskAction="ExecuteReader">

   <Output ItemName="ExportResult" TaskParameter="ReaderResult"/>

</MSBuild.ExtensionPack.SqlServer.SqlExecute>


<WriteLinesToFile File="%(ExportResult.XmlFileName).xml" 
    Lines="&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;;
        %(ExportResult.FileContent)" Overwrite="true"/>

问题是 - 我在单行中获取 xml 数据,这些数据不可读,难以编辑等。

如何获得带有换行符和缩进的人类可读 xml?

谢谢。

4

1 回答 1

0

做了以下,而不是 WriteLinesToFile 任务

<SaveFormattedXml XmlString="%(ExportResult.FileContent)" FilePath="%(ExportResult.XmlFileName).xml"/>

<UsingTask TaskName="SaveFormattedXml" TaskFactory="CodeTaskFactory" AssemblyFile="c:\Program Files (x86)\MSBuild\12.0\Bin\amd64\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup>
  <XmlString ParameterType="System.String" Required="true" />
  <FilePath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
  <Reference Include="System.Xml" />
  <Reference Include="System.Xml.Linq"/>
  <Using Namespace="System" />
  <Using Namespace="System.IO" />
  <Using Namespace="System.Xml" />
  <Using Namespace="System.Xml.Linq" />
  <Code Type="Fragment" Language="cs">
    <![CDATA[
       XDocument doc = XDocument.Parse(XmlString);
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = "  ";
        settings.NewLineChars = "\r\n";
        settings.NewLineHandling = NewLineHandling.Replace;
        using (Stream fileStream = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            using (XmlWriter writer = XmlWriter.Create(fileStream, settings))
            {
                doc.Save(writer);
            }
        }
    ]]>
  </Code>
</Task>

于 2014-10-30T21:23:26.560 回答