xml-text - not usable - .net framework 3.5 / VS2008

  • Thread starter Thread starter Rolf Welskes
  • Start date Start date
R

Rolf Welskes

Hello,
here a simple xml,
<?xml version="1.0" encoding="utf-8" ?>
<a>
this
is
a
little
text
</a>

here a simple program (wpf, but could also be winforms):

<Window x:Class="TestXmlText.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid Background="LightGray">
<TextBox Margin="40,38,47,111" Name="tx" AcceptsReturn="True" />
</Grid>
</Window>

using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestXmlText
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

XmlDocument xdoc = new XmlDocument();
xdoc.Load("..\\..\\TestXml.xml");

XmlNode node = xdoc.ChildNodes[0].NextSibling;

tx.Text = node.InnerText;

}
}
}

So here you see: tx.Text = node.InnerText
This text look like this
this
is
a
little
text.

I want to get:
this is a little text.
No CR, LF, not unnecceary spaces, simle the text entered. (similar as it is
in html)
I cannot find any method or helper class to get the text clean from CRLF,
and the spaces.

Thank you for any help.
Rolf Welskes
 
Hi Rolf,

Based on my research, the XML specification requires that all whitespaces
in element content must be passed to the application.

On the other hand, it's easy to use a simple Regular Expression to achieve
your objective:

static string NormalizeSpace(string input)
{
return Regex.Replace(input, "\\s+", " ").Trim();
}

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top