本文整理汇总了C#中System.Xml.Linq.XText.AddBeforeSelf方法的典型用法代码示例。如果您正苦于以下问题:C# XText.AddBeforeSelf方法的具体用法?C# XText.AddBeforeSelf怎么用?C# XText.AddBeforeSelf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XText
的用法示例。
在下文中一共展示了XText.AddBeforeSelf方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddWordMarkup
/// <summary>
/// Adds word level markup to an <see cref="XText"/>
/// </summary>
/// <param name="textNode">The <see cref="XText"/></param>
/// <param name="wordElementName">The name of the element to mark up words with</param>
/// <param name="wordClassName">The class attribute value for word markup elements</param>
/// <param name="idPrefix">A prefix for id attributes of word markup elements</param>
/// <returns>A <see cref="bool"/> indicating if any word level markup was actually added</returns>
public static bool AddWordMarkup(XText textNode, XName wordElementName, string wordClassName, string idPrefix)
{
var doc = textNode.Document;
var ids = doc == null
? new HashSet<string>()
: new HashSet<string>(doc.Descendants().Attributes(IdAttributeName).Select(a => a.Value));
var expr = new Regex(@"\b\w+\b");
if (expr.Matches(textNode.Value).Count <= 1) return false;
var match = expr.Match(textNode.Value);
while (match.Success)
{
if (match.Index > 0) textNode.AddBeforeSelf(new XText(textNode.Value.Substring(0, match.Index)));
var wordElem = new XElement(wordElementName, GetIdAttr(ids, idPrefix), match.Value);
if (!String.IsNullOrWhiteSpace(wordClassName))
{
wordElem.SetAttributeValue("class", wordClassName);
}
textNode.AddBeforeSelf(wordElem);
textNode.Value = textNode.Value.Substring(match.Index + match.Length);
if (textNode.Value.Length == 0) textNode.Remove();
match = expr.Match(textNode.Value);
}
return true;
}