【问题标题】:Can I get the length of the values in a tag我可以获取标签中值的长度吗
【发布时间】:2016-08-07 08:02:14
【问题描述】:

我有一个以:

开头的 XML 文件
<!DOCTYPE html>
<!-- This is a simple page with no bean -->
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Combo boxes page</title>
</h:head>

为了处理这个文件,我正在做的是:

XElement root = (XElement.Load(fileName, LoadOptions.SetLineInfo | LoadOptions.PreserveWhitespace));

一旦我有了根,我可以得到其中文本的长度吗? (HTML 语言...)

【问题讨论】:

  • 你试过 root.Value.ToString().Length 吗?
  • 是否已解决或是否有澄清/后续问题?

标签: c# xelement


【解决方案1】:

您想要从开始 html 标记 (&lt;html...) 到结束 html 标记 (&lt;/html&gt;) 的字符串长度吗?

XElement root = (XElement.Load(fileName ,LoadOptions.SetLineInfo | LoadOptions.PreserveWhitespace));

int htmlLength = root.ToString().Length;

System.Diagnostics.Debug.WriteLine("length of string including html tags: " + htmlLength);

还是要html标签之间的字符串长度?

String html = root.ToString();
int indexOpeningTag = html.IndexOf(">")+1;
String contentInHtmlTags = html.Substring(indexOpeningTag, html.IndexOf("</html>") - indexOpeningTag);

System.Diagnostics.Debug.WriteLine("length of string between html tags: " + contentInHtmlTags.Length);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    相关资源
    最近更新 更多