I've been looking for a while, but I seem not able to find a solution.. I am building XElement with Linq and everything is being nicely aligned except when I try to linebreak Xelement.Value into multiple lines..
My codes looks like:
var result = new XElement("Items",
new XAttribute("ID", id),
new XElement("ItemsA", $"\n{string.Join(",\n", ItemsA.Select(x => x.Value))}\n"),
new XElement("ItemsB", $"\n{string.Join(",\n", ItemsB.Select(x => x.Value))}\n"));
The output of the result looks like:
<Items ID="123123">
<ItemsA>
items1a,
items2a,
</ItemsA>
<ItemsB>
items1b,
items2b,
</ItemsB>
</Items>
my question is - how can I make it format/indent it correctly?
I have tried changing $"\n\t{string.Join(",\n\t", ItemsA.Select(x => x.Value))}\n\t"
but then it is tabbed further away then the rest of the tags..
What I am trying to achieve is:
<Items ID="123123">
<ItemsA>
items1a,
items2a,
</ItemsA>
<ItemsB>
items1b,
items2b,
</ItemsB>
</Items>
I also tried:
private static XElement FormatXml(XElement input)
{
var settings = new XmlWriterSettings();
settings.Indent = true;
var builder = new StringBuilder(input.ToString());
using(var writer = XmlWriter.Create(builder, settings))
{
input.WriteTo(writer);
}
return input;
}
but it was no luck either.