我可以获取标签中值的长度

时间:2016-08-07 08:02:15

标签: c# xelement

我有一个以:

开头的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语言......)

1 个答案:

答案 0 :(得分:0)

您是否希望以开头html标记(<html...)开头的字符串长度为结束html标记(</html>)?

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