Question

I've an xml string stored in the db table with line feed characters. In my C# 3.5 program, I load and manipulate it using Linq to xml and then show it as a string in the textbox control on the UI form.

I need to indent this xml as well as preserve line feeds/carriage return while showing it in the UI.

Am able to indent it but how do I preserve LF/CR chars in the xml??

Here's the sample C# code:

    XElement rootNode = CreateRootNode();
    XElement testXmlNode = XElement.Parse(xmlFromDbWithLFChars);

    rootNode.Add(testXmlNode );

    var builder = new StringBuilder();
    var settings = new XmlWriterSettings()
    {
     Indent = true
    };

    using (var writer = XmlWriter.Create(builder, settings))
    {
     rootNode.WriteTo(writer);
    }
    xmlString  = builder.ToString();   

    xmlString = xmlString.Replace("
", Environment.NewLine); //Doesnt work

    xmlString = xmlString.Replace("
", Environment.NewLine);  //Doesnt work

//Heres how the xml should look like in the UI control:
 <TestNode
             name="xyz"
             Id="12">
             <Children>
                  <Child name="abc" location="p" />
             </Children>
    </TestNode>
Was it helpful?

Solution

What you want to do is to set the settings of formatting on the XmlWriter, so change your row:

var settings = new XmlWriterSettings() 
    { 
     Indent = true 
    }; 

To something like this:

var settings = new XmlWriterSettings() 
    { 
     Indent = true,
     IndentChars = "\n",
     NewLineOnAttributes = true
    }; 

OTHER TIPS

Thanks all for your responses. Finally,I could get this working.

My approach does not use Linq2Xml/SAX parser.Am generating the xml using StringBuilder and showing it in the UI in a winforms Rich textbox control.Now,I can see line-feeds as it is in the UI.

Any time you convert an XML document to a string and start manipulating the string, you should think to yourself, "Self, I am doing something wrong." I'm not certain from your description if that's true, but I bet it is.

If the whitespace in the XML you're pulling from the database is significant, you want to preserve it when you parse it into your XElement. To do this, use the overload of XElement.Parse that does this, e.g.:

XElement testXmlNode = XElement.Parse(xmlFromDbWithLFChars, LoadOptions.PreserveWhitespace);

When you do this, the parser will leave whitespace characters in the parsed XElement document's text nodes exactly where they were in the original string. XmlWriter doesn't mess with existing whitespace in text nodes (though it will add new whitespace if you tell it to indent), so this should get you what you want.

You can use XmlReader to preserve the new lines and everything.. here is sample code that worked fine for me when testing:

System.Xml.XmlReader reader = System.Xml.XmlReader.Create("XML URI here");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (reader.Read())
{
    sb.Append(reader.ReadOuterXml());
}
reader.Close();
txtXML.InnerText = sb.ToString();
txtXML.Visible = true;

In my test I loaded XML file, you can load your manipulated XML string.

Have you tried making sure the textbox is in multiline mode and accepts carriage return?

public void CreateMyMultilineTextBox() {
   // Create an instance of a TextBox control.
   TextBox textBox1 = new TextBox();

   // Set the Multiline property to true.
   textBox1.Multiline = true;
   // Add vertical scroll bars to the TextBox control.
   textBox1.ScrollBars = ScrollBars.Vertical;
   // Allow the RETURN key to be entered in the TextBox control.
   textBox1.AcceptsReturn = true;
   // Allow the TAB key to be entered in the TextBox control.
   textBox1.AcceptsTab = true;
   // Set WordWrap to true to allow text to wrap to the next line.
   textBox1.WordWrap = true;
   // Set the default text of the control.
   textBox1.Text = "Welcome!";
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top