0

i'm using msbuild for some automation. One of the task is sql query to get xml representation of table and write it to file. So i'm using

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

The problem is - i get the xml data in single line which is not readable, hard to edit etc.

How can i get the human readable xml with line breaks and indents?

Thanks.

lavrik
  • 1,456
  • 14
  • 25

1 Answers1

0

Did the following, instead of WriteLinesToFile Task

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

lavrik
  • 1,456
  • 14
  • 25