private static string ComputeOneLevelOfIndentation(this XNode node) { int num = node.Ancestors().Count <XElement>(); XText previousNode = node.PreviousNode as XText; if ((num == 0) || ((previousNode == null) || !previousNode.IsWhiteSpace())) { return(" "); } string source = previousNode.Value.Trim(Environment.NewLine.ToCharArray()); return(new string((source.LastOrDefault <char>() == '\t') ? '\t' : ' ', Math.Max(1, source.Length / num))); }
public static void RemoveIndented(this XNode element) { XText previousNode = element.PreviousNode as XText; XText nextNode = element.NextNode as XText; string str = element.ComputeOneLevelOfIndentation(); element.Remove(); if ((nextNode != null) && nextNode.IsWhiteSpace()) { nextNode.Remove(); } if (!element.ElementsAfterSelf().Any <XElement>() && ((previousNode != null) && previousNode.IsWhiteSpace())) { previousNode.Value = previousNode.Value.Substring(0, previousNode.Value.Length - str.Length); } }
private static string ComputeOneLevelOfIndentation(this XNode node) { var depth = node.Ancestors().Count(); XText textBeforeOrNull = node.PreviousNode as XText; if (depth == 0 || textBeforeOrNull == null || !textBeforeOrNull.IsWhiteSpace()) { return(" "); } string indentString = textBeforeOrNull.Value.Trim(Environment.NewLine.ToCharArray()); char lastChar = indentString.LastOrDefault(); char indentChar = (lastChar == '\t' ? '\t' : ' '); int indentLevel = Math.Max(1, indentString.Length / depth); return(new string(indentChar, indentLevel)); }
public static void RemoveIndented(this XNode element) { // NOTE: this method is tested by BindinRedirectManagerTest and SettingsTest XText textBeforeOrNull = element.PreviousNode as XText; XText textAfterOrNull = element.NextNode as XText; string oneIndentLevel = element.ComputeOneLevelOfIndentation(); bool isLastChild = !element.ElementsAfterSelf().Any(); element.Remove(); if (textAfterOrNull != null && textAfterOrNull.IsWhiteSpace()) { textAfterOrNull.Remove(); } if (isLastChild && textBeforeOrNull != null && textBeforeOrNull.IsWhiteSpace()) { textBeforeOrNull.Value = textBeforeOrNull.Value.Substring(0, textBeforeOrNull.Value.Length - oneIndentLevel.Length); } }