本文整理汇总了C#中System.Configuration.XmlUtilWriter.AppendIndent方法的典型用法代码示例。如果您正苦于以下问题:C# XmlUtilWriter.AppendIndent方法的具体用法?C# XmlUtilWriter.AppendIndent怎么用?C# XmlUtilWriter.AppendIndent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Configuration.XmlUtilWriter
的用法示例。
在下文中一共展示了XmlUtilWriter.AppendIndent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatXmlElement
// Format an Xml element to be written to the config file.
// Params:
// xmlElement - the element
// linePosition - start position of the element
// indent - indent for each depth
// skipFirstIndent - skip indent for the first element?
//
static internal string FormatXmlElement(string xmlElement, int linePosition, int indent, bool skipFirstIndent) {
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
XmlTextReader reader = new XmlTextReader(xmlElement, XmlNodeType.Element, context);
StringWriter stringWriter = new StringWriter(new StringBuilder(64), CultureInfo.InvariantCulture);
XmlUtilWriter utilWriter = new XmlUtilWriter(stringWriter, false);
// append newline before indent?
bool newLine = false;
// last node visited was text?
bool lastWasText = false;
// width of line from end of indentation
int lineWidth;
// length of the stringbuilder after last indent with newline
int sbLengthLastNewLine = 0;
while (reader.Read()) {
XmlNodeType nodeType = reader.NodeType;
if (lastWasText) {
utilWriter.Flush();
lineWidth = sbLengthLastNewLine - ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
}
else {
lineWidth = 0;
}
switch (nodeType) {
case XmlNodeType.CDATA:
case XmlNodeType.Element:
case XmlNodeType.EndElement:
case XmlNodeType.Comment:
// Do not indent if the last node was text - doing so would add whitespace
// that is included as part of the text.
if (!skipFirstIndent && !lastWasText) {
utilWriter.AppendIndent(linePosition, indent, reader.Depth, newLine);
if (newLine) {
utilWriter.Flush();
sbLengthLastNewLine = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
}
}
break;
default:
break;
}
lastWasText = false;
switch (nodeType) {
case XmlNodeType.Whitespace:
break;
case XmlNodeType.SignificantWhitespace:
utilWriter.Write(reader.Value);
break;
case XmlNodeType.CDATA:
utilWriter.AppendCData(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
utilWriter.AppendProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
utilWriter.AppendComment(reader.Value);
break;
case XmlNodeType.Text:
utilWriter.AppendEscapeTextString(reader.Value);
lastWasText = true;
break;
case XmlNodeType.Element:
{
// Write "<elem"
utilWriter.Write('<');
utilWriter.Write(reader.Name);
lineWidth += reader.Name.Length + 2;
int c = reader.AttributeCount;
for (int i = 0; i < c; i++) {
// Add new line if we've exceeded the line width
bool writeSpace;
if (lineWidth > MAX_LINE_WIDTH) {
utilWriter.AppendIndent(linePosition, indent, reader.Depth - 1, true);
lineWidth = indent;
//.........这里部分代码省略.........
示例2: FormatXmlElement
internal static string FormatXmlElement(string xmlElement, int linePosition, int indent, bool skipFirstIndent)
{
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
XmlTextReader reader = new XmlTextReader(xmlElement, XmlNodeType.Element, context);
StringWriter writer = new StringWriter(new StringBuilder(0x40), CultureInfo.InvariantCulture);
XmlUtilWriter writer2 = new XmlUtilWriter(writer, false);
bool newLine = false;
bool flag2 = false;
int num2 = 0;
while (reader.Read())
{
int num;
int attributeCount;
int num4;
bool flag3;
XmlNodeType nodeType = reader.NodeType;
if (flag2)
{
writer2.Flush();
num = num2 - ((StringWriter) writer2.Writer).GetStringBuilder().Length;
}
else
{
num = 0;
}
XmlNodeType type2 = nodeType;
if (type2 <= XmlNodeType.CDATA)
{
switch (type2)
{
case XmlNodeType.Element:
case XmlNodeType.CDATA:
goto Label_0091;
}
goto Label_00CA;
}
if ((type2 != XmlNodeType.Comment) && (type2 != XmlNodeType.EndElement))
{
goto Label_00CA;
}
Label_0091:
if (!skipFirstIndent && !flag2)
{
writer2.AppendIndent(linePosition, indent, reader.Depth, newLine);
if (newLine)
{
writer2.Flush();
num2 = ((StringWriter) writer2.Writer).GetStringBuilder().Length;
}
}
Label_00CA:
flag2 = false;
switch (nodeType)
{
case XmlNodeType.Element:
writer2.Write('<');
writer2.Write(reader.Name);
num += reader.Name.Length + 2;
attributeCount = reader.AttributeCount;
num4 = 0;
goto Label_0274;
case XmlNodeType.Text:
writer2.AppendEscapeTextString(reader.Value);
flag2 = true;
goto Label_02D6;
case XmlNodeType.CDATA:
writer2.AppendCData(reader.Value);
goto Label_02D6;
case XmlNodeType.EntityReference:
writer2.AppendEntityRef(reader.Name);
goto Label_02D6;
case XmlNodeType.ProcessingInstruction:
writer2.AppendProcessingInstruction(reader.Name, reader.Value);
goto Label_02D6;
case XmlNodeType.Comment:
writer2.AppendComment(reader.Value);
goto Label_02D6;
case XmlNodeType.SignificantWhitespace:
writer2.Write(reader.Value);
goto Label_02D6;
case XmlNodeType.EndElement:
writer2.Write("</");
writer2.Write(reader.Name);
writer2.Write('>');
goto Label_02D6;
default:
goto Label_02D6;
}
Label_01BE:
if (num > 60)
{
writer2.AppendIndent(linePosition, indent, reader.Depth - 1, true);
//.........这里部分代码省略.........