6

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>
user40907
  • 1,532
  • 5
  • 24
  • 33

5 Answers5

3

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
    }; 
Almund
  • 5,695
  • 3
  • 31
  • 35
1

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.

user40907
  • 1,532
  • 5
  • 24
  • 33
  • you should add the solution you used as an answer. – Mauro Nov 17 '10 at 08:09
  • "generating the xml using StringBuilder" - I'm sorry but there is no way in hell this is correct. XML is not string data, attempting to treat it as such whether that's regex parsing or stringbuilder assembling lacks rigour, is brittle for maintenance and is extremely prone to human error. – annakata Nov 17 '10 at 08:27
  • Annakata, what u r saying is true, but,unfortunately I cant give the same justification to business....since I didnt want to go the StringBuilder way knowing that it wud be fragile code, I first tried with Linq2API, SAX and DOM parser...but cudnt get the desired formatted output.Thanks. – user40907 Nov 17 '10 at 10:10
0

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.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • Hi Robert, thanks for your response...but this approach does not to work for me...i still cant see line feeds in the xml in textarea in UI . – user40907 Nov 12 '10 at 12:55
  • 1
    When you say "textarea", do you mean an HTML TEXTAREA? Because new lines in a TEXTAREA element are indicated by CR/LF pairs. If you just have LF characters in the XML text, they won't render as newlines in a TEXTAREA. – Robert Rossney Nov 12 '10 at 18:57
  • Its a textbox control on a .Net Winform. – user40907 Nov 12 '10 at 19:04
  • What I'm saying is that the fact that you can't see line feeds in the textarea doesn't mean they're not present in the XML. If you just send a string like `this\nis\na\ntest` to the UI, does it display with newlines? – Robert Rossney Nov 13 '10 at 00:22
  • I had tried that earlier....no, it doesnt show newlines in the textbox.....Thanks. – user40907 Nov 13 '10 at 10:03
  • 1
    Then this problem doesn't have anything to do with XML, does it? – Robert Rossney Nov 15 '10 at 19:20
  • 1
    daft question but is the text box multiline enabled? and does it accept carriage returns? – Mauro Nov 17 '10 at 08:08
0

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

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!";
 }
Damian
  • 2,709
  • 2
  • 29
  • 40