Skip to content

Commit 00d01c3

Browse files
committed
Added a beautifier function for the XML output so that it can be used directly as is without having to manually beautify the output.
1 parent 3028581 commit 00d01c3

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

SpriteImageParser/SpriteImageParser.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<Title>Sprite Image Parser (SIP)</Title>
1212
<RepositoryType>git</RepositoryType>
1313
<RepositoryUrl>https://github.com/TohnoCoding/SpriteImageParser</RepositoryUrl>
14-
<AssemblyVersion>0.1.1</AssemblyVersion>
15-
<FileVersion>0.1.1</FileVersion>
14+
<AssemblyVersion>0.1.3</AssemblyVersion>
15+
<FileVersion>0.1.3</FileVersion>
1616
<PackAsTool>True</PackAsTool>
1717
<PackageTags>game helper;game development;game programming;game toolkit;MonoGame;MonoGame tool</PackageTags>
1818
<Company>TohnoCoding</Company>

SpriteImageParser/SpriteRegionExporter.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#pragma warning disable // CS8602 is being unnecessarily annoying in the XML builder
2+
using System.Runtime.CompilerServices;
3+
using System.Text;
24
using System.Text.Json;
35
using System.Xml;
46

@@ -80,7 +82,7 @@ public static string SerializeToXml(List<SpriteRegion> regions, string frameName
8082
root.AppendChild(regionElement);
8183
counter++;
8284
}
83-
return doc.OuterXml;
85+
return Beautify(doc);
8486
}
8587

8688
/// <summary>
@@ -89,5 +91,25 @@ public static string SerializeToXml(List<SpriteRegion> regions, string frameName
8991
/// <param name="regions">The list of sprite regions to serialize.</param>
9092
/// <returns>An XML string representing the sprite regions provided.</returns>
9193
public static string SerializeToXml(List<SpriteRegion> regions) => SerializeToXml(regions, "Frame", 1);
94+
95+
/// <summary>
96+
/// Beautifies an XML document by formatting it with indentation and new lines.
97+
/// </summary>
98+
/// <param name="doc">The XML document to beautify.</param>
99+
/// <returns>A string representation of the XML document with beautified indentation.</returns>
100+
static private string Beautify(XmlDocument doc)
101+
{
102+
StringBuilder sb = new();
103+
XmlWriterSettings settings = new()
104+
{
105+
Indent = true,
106+
NewLineHandling = NewLineHandling.Replace,
107+
NewLineChars = "\r\n",
108+
IndentChars = " "
109+
};
110+
using (XmlWriter writer = XmlWriter.Create(sb, settings))
111+
{ doc.Save(writer); }
112+
return sb.ToString();
113+
}
92114
}
93115
}

0 commit comments

Comments
 (0)