private void ProcessElement(XmlReader xmlReader, StringBuilder output) { string currentIndentString = GetIndentString(xmlReader.Depth); string elementName = xmlReader.Name; // Calculate how element should be indented if (!_elementProcessStatusStack.Peek().IsPreservingSpace) { // "Run" get special treatment to try to preserve spacing. Use xml:space='preserve' to make sure! if (elementName.Equals("Run")) { _elementProcessStatusStack.Peek().Parent.IsSignificantWhiteSpace = true; if (output.Length == 0 || output.IsNewLine()) { output.Append(currentIndentString); } } else { _elementProcessStatusStack.Peek().Parent.IsSignificantWhiteSpace = false; if (output.Length == 0 || output.IsNewLine()) { output.Append(currentIndentString); } else { output .Append(Environment.NewLine) .Append(currentIndentString); } } } // Output the element itself output .Append('<') .Append(xmlReader.Name); bool isEmptyElement = xmlReader.IsEmptyElement; bool hasPutEndingBracketOnNewLine = false; var list = new List <AttributeInfo>(xmlReader.AttributeCount); if (xmlReader.HasAttributes) { while (xmlReader.MoveToNextAttribute()) { string attributeName = xmlReader.Name; string attributeValue = xmlReader.Value; AttributeOrderRule orderRule = OrderRules.GetRuleFor(attributeName); list.Add(new AttributeInfo(attributeName, attributeValue, orderRule)); // Check for xml:space as defined in http://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space if (xmlReader.IsXmlSpaceAttribute()) { _elementProcessStatusStack.Peek().IsPreservingSpace = (xmlReader.Value == "preserve"); } } if (Options.EnableAttributeReordering) { list.Sort(AttributeInfoComparison); } currentIndentString = GetIndentString(xmlReader.Depth); var noLineBreakInAttributes = (list.Count <= Options.AttributesTolerance) || IsNoLineBreakElement(elementName); var forceLineBreakInAttributes = false; // Root element? if (_elementProcessStatusStack.Count == 2) { switch (Options.RootElementLineBreakRule) { case LineBreakRule.Default: break; case LineBreakRule.Always: noLineBreakInAttributes = false; forceLineBreakInAttributes = true; break; case LineBreakRule.Never: noLineBreakInAttributes = true; break; default: throw new ArgumentOutOfRangeException(); } } // No need to break attributes if (noLineBreakInAttributes) { foreach (var attrInfo in list) { output .Append(' ') .Append(attrInfo.ToSingleLineString()); } _elementProcessStatusStack.Peek().IsMultlineStartTag = false; } // Need to break attributes else { IList <String> attributeLines = new List <String>(); var currentLineBuffer = new StringBuilder(); int attributeCountInCurrentLineBuffer = 0; AttributeInfo lastAttributeInfo = null; foreach (AttributeInfo attrInfo in list) { // Attributes with markup extension, always put on new line if (attrInfo.IsMarkupExtension && Options.FormatMarkupExtension) { string baseIndetationString; if (!Options.KeepFirstAttributeOnSameLine) { baseIndetationString = GetIndentString(xmlReader.Depth); } else { baseIndetationString = GetIndentString(xmlReader.Depth - 1) + string.Empty.PadLeft(elementName.Length + 2, ' '); } string pendingAppend; if (NoNewLineMarkupExtensionsList.Contains(attrInfo.MarkupExtension)) { pendingAppend = " " + attrInfo.ToSingleLineString(); } else { pendingAppend = attrInfo.ToMultiLineString(baseIndetationString); } if (currentLineBuffer.Length > 0) { attributeLines.Add(currentLineBuffer.ToString()); currentLineBuffer.Length = 0; attributeCountInCurrentLineBuffer = 0; } attributeLines.Add(pendingAppend); } else { string pendingAppend = attrInfo.ToSingleLineString(); bool isAttributeCharLengthExceeded = (attributeCountInCurrentLineBuffer > 0 && Options.MaxAttributeCharatersPerLine > 0 && currentLineBuffer.Length + pendingAppend.Length > Options.MaxAttributeCharatersPerLine); bool isAttributeCountExceeded = (Options.MaxAttributesPerLine > 0 && attributeCountInCurrentLineBuffer + 1 > Options.MaxAttributesPerLine); bool isAttributeRuleGroupChanged = Options.PutAttributeOrderRuleGroupsOnSeparateLines && lastAttributeInfo != null && lastAttributeInfo.OrderRule.Group != attrInfo.OrderRule.Group; if (currentLineBuffer.Length > 0 && (forceLineBreakInAttributes || isAttributeCharLengthExceeded || isAttributeCountExceeded || isAttributeRuleGroupChanged)) { attributeLines.Add(currentLineBuffer.ToString()); currentLineBuffer.Length = 0; attributeCountInCurrentLineBuffer = 0; } currentLineBuffer.AppendFormat("{0} ", pendingAppend); attributeCountInCurrentLineBuffer++; } lastAttributeInfo = attrInfo; } if (currentLineBuffer.Length > 0) { attributeLines.Add(currentLineBuffer.ToString()); } for (int i = 0; i < attributeLines.Count; i++) { if (0 == i && Options.KeepFirstAttributeOnSameLine) { output .Append(' ') .Append(attributeLines[i].Trim()); // Align subsequent attributes with first attribute currentIndentString = GetIndentString(xmlReader.Depth - 1) + String.Empty.PadLeft(elementName.Length + 2, ' '); continue; } output .Append(Environment.NewLine) .Append(currentIndentString) .Append(attributeLines[i].Trim()); } _elementProcessStatusStack.Peek().IsMultlineStartTag = true; } // Determine if to put ending bracket on new line if (Options.PutEndingBracketOnNewLine && _elementProcessStatusStack.Peek().IsMultlineStartTag) { output .Append(Environment.NewLine) .Append(currentIndentString); hasPutEndingBracketOnNewLine = true; } } if (isEmptyElement) { if (hasPutEndingBracketOnNewLine == false && Options.SpaceBeforeClosingSlash) { output.Append(' '); } output.Append("/>"); _elementProcessStatusStack.Peek().IsSelfClosingElement = true; } else { output.Append(">"); } }
private void ProcessElement(XmlReader xmlReader, StringBuilder output) { string currentIndentString = GetIndentString(xmlReader.Depth); string elementName = xmlReader.Name; if ("Run".Equals(elementName)) { if (output.IsNewLine()) { // Shall not add extra whitespaces (including linefeeds) before <Run/>, // because it will affect the rendering of <TextBlock><Run/><Run/></TextBlock> output .Append(currentIndentString) .Append('<') .Append(xmlReader.Name); } else { output.Append('<'); output.Append(xmlReader.Name); } } else if (output.Length == 0 || output.IsNewLine()) { output .Append(currentIndentString) .Append('<') .Append(xmlReader.Name); } else { output .Append(Environment.NewLine) .Append(currentIndentString) .Append('<') .Append(xmlReader.Name); } bool isEmptyElement = xmlReader.IsEmptyElement; bool hasPutEndingBracketOnNewLine = false; var list = new List <AttributeInfo>(xmlReader.AttributeCount); if (xmlReader.HasAttributes) { while (xmlReader.MoveToNextAttribute()) { string attributeName = xmlReader.Name; string attributeValue = xmlReader.Value; AttributeOrderRule orderRule = OrderRules.GetRuleFor(attributeName); list.Add(new AttributeInfo(attributeName, attributeValue, orderRule)); } if (Options.OrderAttributesByName) { list.Sort(); } currentIndentString = GetIndentString(xmlReader.Depth); var noLineBreakInAttributes = (list.Count <= Options.AttributesTolerance) || IsNoLineBreakElement(elementName); // Root element? if (_elementProcessStatusStack.Count == 2) { switch (Options.RootElementLineBreakRule) { case LineBreakRule.Default: break; case LineBreakRule.Always: noLineBreakInAttributes = false; break; case LineBreakRule.Never: noLineBreakInAttributes = true; break; default: throw new ArgumentOutOfRangeException(); } } // No need to break attributes if (noLineBreakInAttributes) { foreach (var attrInfo in list) { output .Append(' ') .Append(attrInfo.ToSingleLineString()); } _elementProcessStatusStack.Peek().IsMultlineStartTag = false; } // Need to break attributes else { IList <String> attributeLines = new List <String>(); var currentLineBuffer = new StringBuilder(); int attributeCountInCurrentLineBuffer = 0; AttributeInfo lastAttributeInfo = null; foreach (AttributeInfo attrInfo in list) { // Attributes with markup extension, always put on new line if (attrInfo.IsMarkupExtension && Options.FormatMarkupExtension) { string baseIndetationString; if (!Options.KeepFirstAttributeOnSameLine) { baseIndetationString = GetIndentString(xmlReader.Depth); } else { baseIndetationString = GetIndentString(xmlReader.Depth - 1) + string.Empty.PadLeft(elementName.Length + 2, ' '); } string pendingAppend; //Keep binding and / or x:bind on same line? if ((attrInfo.Value.ToLower().Contains("x:bind ") && Options.KeepxBindOnSameLine) || Options.KeepBindingsOnSameLine) { pendingAppend = " " + attrInfo.ToSingleLineString(); } else { pendingAppend = attrInfo.ToMultiLineString(baseIndetationString); } if (currentLineBuffer.Length > 0) { attributeLines.Add(currentLineBuffer.ToString()); currentLineBuffer.Length = 0; attributeCountInCurrentLineBuffer = 0; } attributeLines.Add(pendingAppend); } else { string pendingAppend = attrInfo.ToSingleLineString(); bool isAttributeCharLengthExceeded = (attributeCountInCurrentLineBuffer > 0 && Options.MaxAttributeCharatersPerLine > 0 && currentLineBuffer.Length + pendingAppend.Length > Options.MaxAttributeCharatersPerLine); bool isAttributeCountExceeded = (Options.MaxAttributesPerLine > 0 && attributeCountInCurrentLineBuffer + 1 > Options.MaxAttributesPerLine); bool isAttributeRuleGroupChanged = Options.PutAttributeOrderRuleGroupsOnSeparateLines && lastAttributeInfo != null && lastAttributeInfo.OrderRule.AttributeTokenType != attrInfo.OrderRule.AttributeTokenType; if (isAttributeCharLengthExceeded || isAttributeCountExceeded || isAttributeRuleGroupChanged) { attributeLines.Add(currentLineBuffer.ToString()); currentLineBuffer.Length = 0; attributeCountInCurrentLineBuffer = 0; } currentLineBuffer.AppendFormat("{0} ", pendingAppend); attributeCountInCurrentLineBuffer++; } lastAttributeInfo = attrInfo; } if (currentLineBuffer.Length > 0) { attributeLines.Add(currentLineBuffer.ToString()); } for (int i = 0; i < attributeLines.Count; i++) { if (0 == i && Options.KeepFirstAttributeOnSameLine) { output .Append(' ') .Append(attributeLines[i].Trim()); // Align subsequent attributes with first attribute currentIndentString = GetIndentString(xmlReader.Depth - 1) + String.Empty.PadLeft(elementName.Length + 2, ' '); continue; } output .Append(Environment.NewLine) .Append(currentIndentString) .Append(attributeLines[i].Trim()); } _elementProcessStatusStack.Peek().IsMultlineStartTag = true; } // Determine if to put ending bracket on new line if (Options.PutEndingBracketOnNewLine && _elementProcessStatusStack.Peek().IsMultlineStartTag) { output .Append(Environment.NewLine) .Append(currentIndentString); hasPutEndingBracketOnNewLine = true; } } if (isEmptyElement) { if (hasPutEndingBracketOnNewLine == false && Options.SpaceBeforeClosingSlash) { output.Append(' '); } output.Append("/>"); _elementProcessStatusStack.Peek().IsSelfClosingElement = true; } else { output.Append(">"); } }