如何删除标签之间的换行符?

发布于 2025-01-19 09:56:17 字数 568 浏览 3 评论 0原文

假设我有这段代码:

        XmlNode counsel = doc.CreateElement("COUNSELS");
        XmlNode p = doc.CreateElement("p");
        counsel.AppendChild(p);
        XmlNode b = doc.CreateElement("b");
        p.AppendChild(b);
        b.InnerText = "Counsel:";

它将输出:

<COUNSELS>
  <p>
    <b>Counsel:</b>
  </p>
</COUNSELS>

我该怎么做才能实现这一点:

<COUNSELS>
  <p><b>Counsel:</b></p>
</COUNSELS>

如果我在写这篇文章时犯了任何错误,请纠正我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2025-01-26 09:56:17

由于OP稳固的正则替换是可以接受的。我可以使用两个简单的替代品。

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);

输出

<COUNSELS>
  <p><b>Counsel:</b></p>
</COUNSELS>

您可以遵循模式以替换其他标签周围的空间,或者使用更高级的单个时间替换,这一切都取决于您的实际需求和技能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文