سؤال

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]]>&lt;br /&gt;<![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

هل كانت مفيدة؟

المحلول

Since HtmlDecode converts &lt; and &gt; to < and >, you might try replacing with the HTML space character &nbsp; instead. I.e.

text = text.Replace("&lt;br /&gt;", "&nbsp;");
                                     ^

نصائح أخرى

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top