stripping newlines from xml

N

Nirmal Singh

I am reading in a text node from an XML file which is on several lines.

How can I get read of newline characters from this string?

NiMuSi
 
M

Morten Wennevik [C# MVP]

Hi,

There are a few options. You can simply use
string.Replace(Environment.Newline, " "). If you want to keep the xml nicely
formatted you can format it using an XmlWriter. You can also create a simple
recursive parser that simply removes the line breaks from just text nodes.

protected override void OnLoad(EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode node in doc.ChildNodes)
StripRN(node);
Console.Write(doc.InnerXml);
}

void StripRN(XmlNode node)
{
if (node.NodeType == XmlNodeType.Text)
node.Value = node.Value.Replace(Environment.NewLine, " ");

foreach (XmlNode childNode in node.ChildNodes)
StripRN(childNode);
}

Depending on your xml you may encounter
 
N

Nirmal Singh

Hello Morten,

Thanks for that, but I am still getting the newlines.

This is my code (with a button and a textbox):

private void button1_Click(object sender, EventArgs e)

{

XmlDocument xmlDoc1 = new XmlDocument();

xmlDoc1.Load(filename);

XmlNode myNode = xmlDoc1.DocumentElement.SelectSingleNode("Description");

StripRN(myNode);

string description = xmlDoc1.DocumentElement.SelectSingleNode("Description").InnerXml;

StringBuilder myString = new StringBuilder();

for (int i = 0; i < description.Length; i++)

{

if (Convert.ToInt16(description) < 32) myString.Append(Convert.ToString(Convert.ToInt16(description)));

else myString.Append(description);

}

textBox1.Text = myString.ToString();

}

void StripRN(XmlNode node)

{

if (node.NodeType == XmlNodeType.Text) node.Value = node.Value.Replace(Environment.NewLine, " ");

}

When I run this (test) I can still see characters 13 and 10 in the output.

I would appreciate any help.




Morten Wennevik said:
Hi,

There are a few options. You can simply use
string.Replace(Environment.Newline, " "). If you want to keep the xml nicely
formatted you can format it using an XmlWriter. You can also create a simple
recursive parser that simply removes the line breaks from just text nodes.

protected override void OnLoad(EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode node in doc.ChildNodes)
StripRN(node);
Console.Write(doc.InnerXml);
}

void StripRN(XmlNode node)
{
if (node.NodeType == XmlNodeType.Text)
node.Value = node.Value.Replace(Environment.NewLine, " ");

foreach (XmlNode childNode in node.ChildNodes)
StripRN(childNode);
}

Depending on your xml you may encounter

--
Happy Coding!
Morten Wennevik [C# MVP]


Nirmal Singh said:
I am reading in a text node from an XML file which is on several lines.

How can I get read of newline characters from this string?

NiMuSi
 
I

Ignacio Machin ( .NET/ C# MVP )

Do this change:

    string description = stripNR(xmlDoc1.DocumentElement.SelectSingleNode("Description").InnerXml);

I'm not pretty sure what are you doing in the for loop though. I bet
there is a better way without the calls to ConvertToXXX
 
L

Luc E. Mistiaen

Your node selection returns an element node and not a text node (which would
be a sub-node of an element node). So your strip function does nothing (as
it checks for text node). You could replace on the whole InnerText at that
level.

Note that your loop with the StringBuilder doesn't do much either.

/LM

Hello Morten,

Thanks for that, but I am still getting the newlines.

This is my code (with a button and a textbox):

private void button1_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc1 = new XmlDocument();
xmlDoc1.Load(filename);
XmlNode myNode =
xmlDoc1.DocumentElement.SelectSingleNode("Description");
StripRN(myNode);
string description =
xmlDoc1.DocumentElement.SelectSingleNode("Description").InnerXml;
StringBuilder myString = new StringBuilder();
for (int i = 0; i < description.Length; i++)
{
if (Convert.ToInt16(description) < 32)
myString.Append(Convert.ToString(Convert.ToInt16(description)));
else myString.Append(description);
}
textBox1.Text = myString.ToString();
}
void StripRN(XmlNode node)
{
if (node.NodeType == XmlNodeType.Text) node.Value =
node.Value.Replace(Environment.NewLine, " ");
}
When I run this (test) I can still see characters 13 and 10 in the output.
I would appreciate any help.


Morten Wennevik said:
Hi,

There are a few options. You can simply use
string.Replace(Environment.Newline, " "). If you want to keep the xml
nicely
formatted you can format it using an XmlWriter. You can also create a
simple
recursive parser that simply removes the line breaks from just text nodes.

protected override void OnLoad(EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode node in doc.ChildNodes)
StripRN(node);
Console.Write(doc.InnerXml);
}

void StripRN(XmlNode node)
{
if (node.NodeType == XmlNodeType.Text)
node.Value = node.Value.Replace(Environment.NewLine, " ");

foreach (XmlNode childNode in node.ChildNodes)
StripRN(childNode);
}

Depending on your xml you may encounter

--
Happy Coding!
Morten Wennevik [C# MVP]


Nirmal Singh said:
I am reading in a text node from an XML file which is on several lines.

How can I get read of newline characters from this string?

NiMuSi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top