Since HtmlDecode converts <
and >
to <
and >
, you might try replacing with the HTML space character
instead. I.e.
text = text.Replace("<br />", " ");
^
Pregunta
I was trying to include space in output when processing xml in C#. Here's What I have
text = text.Replace("<br />", " ");
text = HttpUtility.HtmlDecode(text);
System.Xml.XmlDocument doc = new XmlDocument();
doc.LoadXml(text);
StringBuilder sb = new StringBuilder();
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
sb.Append(' ');
sb.AppendLine(node.InnerText);
}
<root><Paragraph>
<![CDATA[ Electrical Technologies]]><br /><![CDATA[Thomas Edison]]> </Paragraph></root>
I want the output to be Electrical TechnologiesSPACEThomas Edison
but the output is Electrical TechnologiesThomas Edison
.
Any suggestions.
Thanks R
Solución
Since HtmlDecode converts <
and >
to <
and >
, you might try replacing with the HTML space character
instead. I.e.
text = text.Replace("<br />", " ");
^
Otros consejos
I suggest to use Linq to Xml for parsing xml. Here is the way to extract CDATA values:
var xdoc = XDocument.Load(path_to_xml);
var values = xdoc.Root.Element("Paragraph").Nodes()
.OfType<XCData>()
.Select(x => x.Value.Trim());
If you want to get values of all CDATA nodes in this xml file:
var values = xdoc.DescendantNodes().OfType<XCData>().Select(x => x.Value.Trim());
This query returns two items:
[ "Electrical Technologies", "Thomas Edison" ]
You can format output as you wish. E.g. joining values with spaces in one string:
var result = String.Join(" ", values);