2014-10-30 62 views
0

我爲某些自動化使用msbuild。其中一個任務是使用sql查詢來獲取表的xml表示並將其寫入文件。所以我使用T-SQL和MSBUILD - 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和縮進?

謝謝。

回答

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>