我正在使用C#创建一个轻量级编辑器,想知道将字符串转换为格式良好的XML字符串的最佳方法。我希望在C#库中有一个公共方法类似于"public bool FormatAsXml(string text, out string formattedXmlText)",但是事情可能没有那么简单吧?
具体来说,如果要生成下面的输出,方法"SomeMethod"应该是什么?
string unformattedXml;
string formattedXml;
unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"
formattedXml = SomeMethod(unformattedXml);
Console.WriteLine(formattedXml);
输出:
<?xml version="1.0"?>
<book id="123">
<author>Lewis, C.S.</author>
<title>The Four Loves</title>
</book>