我正在使用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="<?xml version="1.0" standalone="yes"?>;
%(ExportResult.FileContent)" Overwrite="true"/>
问题是 - 我用单行获取xml数据,这些数据不可读,难以编辑等。
如何通过换行符和缩进来获取人类可读的xml?
感谢。
答案 0 :(得分: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>