For a portion of my XDocument that looks like this
<name>
xEdKXBh
<first>kryrIQvg</first>
<family>kHymPPId</family>
</name>
I'm modifying that particular branch of nodes like this
switch (node.NodeType)
{
case XmlNodeType.Text:
var xtext = (XText)node;
xtext.Value = "somstring"
break;
case XmlNodeType.Element:
var xelement = (XElement)node;
xelement.Value = "somstring"
break;
default:
//Console.WriteLine(node);
break;
}
But no matter what I've tried, the section I modify always ends up like this, but the rest of the document remains untouched with perfect formatting.
<name>
xEdKXBh
<first>kryrIQvg</first><family>kHymPPId</family></name>
I've tried just using XDocument.ToString(), I've tried using the XmlTextWriter with the Formattign Indented option, and even this method I found in another post here
//StringBuilder sb = new StringBuilder();
//XmlWriterSettings settings = new XmlWriterSettings();
//settings.Indent = true;
//settings.IndentChars = " ";
//settings.NewLineChars = "\r\n";
//settings.NewLineHandling = NewLineHandling.Replace;
//using (XmlWriter writer = XmlWriter.Create(sb, settings))
//{
// doc.Save(writer);
//}
Nothing seems to work.
Why does modifying those nodes remove the formatting to begin with?
And how can I prevent this or fix it in the output?
EDIT: I found that the node that had a text node is what messes up the remaining formatting of the nodes. When I remove the text node, the formatting looks as it should, properly indented. But with the text node, the entire branch of descendants is treated like one long text node with no formatting applied.