Fix UTF-8 output

This commit is contained in:
Matt Nadareski
2016-04-05 18:09:19 -07:00
parent 8c37d050a5
commit bce9b1f863

View File

@@ -27,8 +27,8 @@ namespace DatSplit
// Set needed strings // Set needed strings
filename = args[0]; filename = args[0];
extA = args[1]; extA = (args[1].StartsWith(".") ? args[1] : "." + args[1]);
extB = args[2]; extB = (args[2].StartsWith(".") ? args[2] : "." + args[2]);
// Take the filename, and load it as an XML document // Take the filename, and load it as an XML document
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
@@ -52,9 +52,15 @@ namespace DatSplit
} }
} }
XmlDocument tempDoc = new XmlDocument(); XmlDocument outDocA = new XmlDocument();
XmlNode outA = tempDoc.CreateNode(XmlNodeType.Element, node.Name, ""); outDocA.AppendChild(outDocA.CreateXmlDeclaration("1.0", Encoding.UTF8.WebName, null));
XmlNode outB = tempDoc.CreateNode(XmlNodeType.Element, node.Name, ""); outDocA.AppendChild(outDocA.CreateDocumentType("datafile", "-//Logiqx//DTD ROM Management Datafile//EN", "http://www.logiqx.com/Dats/datafile.dtd", null));
XmlNode outA = outDocA.CreateNode(XmlNodeType.Element, node.Name, "");
XmlDocument outDocB = new XmlDocument();
outDocB.AppendChild(outDocB.CreateXmlDeclaration("1.0", Encoding.UTF8.WebName, null));
outDocB.AppendChild(outDocB.CreateDocumentType("datafile", "-//Logiqx//DTD ROM Management Datafile//EN", "http://www.logiqx.com/Dats/datafile.dtd", null));
XmlNode outB = outDocB.CreateNode(XmlNodeType.Element, node.Name, "");
// Once we find the main body, enter it // Once we find the main body, enter it
if (node != null && node.Name == "datafile") if (node != null && node.Name == "datafile")
@@ -96,29 +102,29 @@ namespace DatSplit
if (!inA) if (!inA)
{ {
//XmlNode temp = tempDoc.CreateNode(XmlNodeType.Element, node.Name, ""); //XmlNode temp = tempDoc.CreateNode(XmlNodeType.Element, node.Name, "");
XmlNode temp = tempDoc.ImportNode(node, false); XmlNode temp = outDocA.ImportNode(node, false);
outA.AppendChild(temp); outA.AppendChild(temp);
outA = outA.LastChild; outA = outA.LastChild;
inA = true; inA = true;
} }
outA.AppendChild(tempDoc.ImportNode(child, true)); outA.AppendChild(outDocA.ImportNode(child, true));
} }
else if (child.Attributes["name"].Value.EndsWith(extB)) else if (child.Attributes["name"].Value.EndsWith(extB))
{ {
if (!inB) if (!inB)
{ {
//XmlNode temp = tempDoc.CreateNode(XmlNodeType.Element, node.Name, ""); //XmlNode temp = tempDoc.CreateNode(XmlNodeType.Element, node.Name, "");
XmlNode temp = tempDoc.ImportNode(node, false); XmlNode temp = outDocB.ImportNode(node, false);
outB.AppendChild(temp); outB.AppendChild(temp);
outB = outB.LastChild; outB = outB.LastChild;
inB = true; inB = true;
} }
outB.AppendChild(tempDoc.ImportNode(child, true)); outB.AppendChild(outDocB.ImportNode(child, true));
} }
else else
{ {
outA.AppendChild(tempDoc.ImportNode(child, true)); outA.AppendChild(outDocA.ImportNode(child, true));
outB.AppendChild(tempDoc.ImportNode(child, true)); outB.AppendChild(outDocB.ImportNode(child, true));
} }
} }
} }
@@ -136,25 +142,22 @@ namespace DatSplit
} }
else else
{ {
XmlNode tempNode = tempDoc.ImportNode(node, true); XmlNode tempNode = outDocA.ImportNode(node, true);
outA.AppendChild(tempNode); outA.AppendChild(tempNode);
tempNode = tempDoc.ImportNode(node, true); tempNode = outDocB.ImportNode(node, true);
outB.AppendChild(tempNode); outB.AppendChild(tempNode);
} }
node = node.NextSibling; node = node.NextSibling;
} }
XmlDocument outDocA = new XmlDocument(); // Append the built nodes to the documents
outDocA.AppendChild(outDocA.CreateDocumentType("datafile", "-//Logiqx//DTD ROM Management Datafile//EN", "http://www.logiqx.com/Dats/datafile.dtd", null));
outDocA.AppendChild(outDocA.ImportNode(outA, true)); outDocA.AppendChild(outDocA.ImportNode(outA, true));
string outPathA = Path.GetFileNameWithoutExtension(filename) + extA + Path.GetExtension(filename); string outPathA = Path.GetFileNameWithoutExtension(filename) + extA + Path.GetExtension(filename);
File.WriteAllText(outPathA, Beautify(outDocA)); File.WriteAllText(outPathA, Beautify(outDocA), Encoding.UTF8);
XmlDocument outDocB = new XmlDocument();
outDocB.AppendChild(outDocB.CreateDocumentType("datafile", "-//Logiqx//DTD ROM Management Datafile//EN", "http://www.logiqx.com/Dats/datafile.dtd", null));
outDocB.AppendChild(outDocB.ImportNode(outB, true)); outDocB.AppendChild(outDocB.ImportNode(outB, true));
string outPathB = Path.GetFileNameWithoutExtension(filename) + extB + Path.GetExtension(filename); string outPathB = Path.GetFileNameWithoutExtension(filename) + extB + Path.GetExtension(filename);
File.WriteAllText(outPathB, Beautify(outDocB)); File.WriteAllText(outPathB, Beautify(outDocB), Encoding.UTF8);
} }
public static void Help() public static void Help()
@@ -163,21 +166,24 @@ namespace DatSplit
} }
// http://stackoverflow.com/questions/203528/what-is-the-simplest-way-to-get-indented-xml-with-line-breaks-from-xmldocument // http://stackoverflow.com/questions/203528/what-is-the-simplest-way-to-get-indented-xml-with-line-breaks-from-xmldocument
// http://www.timvw.be/2007/01/08/generating-utf-8-with-systemxmlxmlwriter/
static public string Beautify(XmlDocument doc) static public string Beautify(XmlDocument doc)
{ {
StringBuilder sb = new StringBuilder(); MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings XmlWriterSettings settings = new XmlWriterSettings
{ {
Encoding = new UTF8Encoding(false),
Indent = true, Indent = true,
IndentChars = "\t", IndentChars = "\t",
NewLineChars = "\r\n", NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace NewLineHandling = NewLineHandling.Replace
}; };
using (XmlWriter writer = XmlWriter.Create(sb, settings))
using (XmlWriter writer = XmlWriter.Create(ms, settings))
{ {
doc.Save(writer); doc.Save(writer);
} }
return sb.ToString(); return Encoding.UTF8.GetString(ms.ToArray());
} }
} }
} }