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
How can I get read of newline characters from this string?
NiMuSi
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
string description = stripNR(xmlDoc1.DocumentElement.SelectSingleNode("Description").InnerXml);
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
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.