Let say i have this code:
XmlNode counsel = doc.CreateElement("COUNSELS");
XmlNode p = doc.CreateElement("p");
counsel.AppendChild(p);
XmlNode b = doc.CreateElement("b");
p.AppendChild(b);
b.InnerText = "Counsel:";
and it will output:
<COUNSELS>
<p>
<b>Counsel:</b>
</p>
</COUNSELS>
what can i do to achieve this:
<COUNSELS>
<p><b>Counsel:</b></p>
</COUNSELS>
correct me if i made any mistake on writing this post.
CodePudding user response:
Since the OP comfirms regex replace is acceptable. I can use two simple replaces.
using System.Text.RegularExpressions;
Regex reg1 = new Regex(@"<p>\s ", RegexOptions.Compiled);
Regex reg2 = new Regex(@"\s </p>", RegexOptions.Compiled);
string xml = @"<COUNSELS>
<p>
<b>Counsel:</b>
</p>
</COUNSELS>";
xml = reg1.Replace(xml, "<p>");
xml = reg2.Replace(xml, "</p>");
Console.WriteLine(xml);
output
<COUNSELS>
<p><b>Counsel:</b></p>
</COUNSELS>
You can follow the pattern to replace space around other tags, or use more advanced single time replace, all depend on your actual requirement and skills.