0

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.

erotavlas
  • 4,274
  • 4
  • 45
  • 104

2 Answers2

0

I am not sure if this is the entire problem you have, but in case of text node (XText), whitespaces (line-breaks and indentations) are part of the node value.

So if you want to preserve them, try to replace only non-whitespace part of the text node value, for example :

case XmlNodeType.Text:
    var xtext = (XText)node;
    //get non-whitespace part
    var oldValue = xtext.Value.Trim();
    //replace only non-whitespace part
    xtext.Value = xtext.Value.Replace(oldValue, "some string");
    break;
har07
  • 88,338
  • 12
  • 84
  • 137
  • its not a problem with the text Nodes, I understand that I'm responsible for handling white space and line breaks in the text. But its the line breaks between nodes that gets messed up when I modify the nodes. Something seems to be happening like the node branch is being written back to the XDocument with modified formatting (indent) – erotavlas May 05 '14 at 14:12
  • I found something else weird, I discovered that it wasn't my modifications to the XElement that cause the formatting issue, it happened right when it was read. Just because there was text inside the tag before the tag is what caused the remaining tags to have their formattign removed. WHen I remove that text and just have child nodes inside the formatting is unchanged and it works – erotavlas May 05 '14 at 14:30
0

This seems to fix it - adding LoadOptions.PreserveWhitespace

XDocument DocumentRoot = XDocument.Parse(inputValue,LoadOptions.PreserveWhitespace);
erotavlas
  • 4,274
  • 4
  • 45
  • 104