I have an xml file which is like so.
<Root>
<Child123>
more nodes inside
</Child123>
<Child123></Child123>
<Child123></Child123>
<Child123></Child123>
</Root>
My code is generating this file which is not correctly formatting its forgetting to place a hard return at only the <Cild123>
. You will see on line 2 the 3 line Child123 is starting on Line 2 when it should be on Line 3?
<Root>
<Child123></Child123>
<Child123></Child123><Child123>
more nodes inside
</Child123><Child123>
more nodes inside
</Child123>
</Root>
This is my code I read the file into a list view and allow the user to pick some lines then I click generate, this then allows the above file to be generated
public static string Seralize<T>(T dataToSerlize)
{
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var seralize = new XmlSerializer(dataToSerlize.GetType());
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineChars = "\n";
settings.NewLineHandling = NewLineHandling.Replace;
using (var stream = new StringWriter())
{
using (var test = XmlWriter.Create(stream, settings))
{
seralize.Serialize(test, dataToSerlize, ns);
return stream.ToString();
}
}
}
But as you see its not keeping the formatting correct in the generated xml file how to I ensure that it retains the flow of the first xml
PS I also tried
settings.Encoding = Encoding.UTF-8;
Which I thought may be the issue.
I also tried
settings.NewLineChars = "\n";
But still no joy.