当前位置: 首页>>代码示例>>C#>>正文


C# XmlUtilWriter.AppendAttributeValue方法代码示例

本文整理汇总了C#中System.Configuration.XmlUtilWriter.AppendAttributeValue方法的典型用法代码示例。如果您正苦于以下问题:C# XmlUtilWriter.AppendAttributeValue方法的具体用法?C# XmlUtilWriter.AppendAttributeValue怎么用?C# XmlUtilWriter.AppendAttributeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Configuration.XmlUtilWriter的用法示例。


在下文中一共展示了XmlUtilWriter.AppendAttributeValue方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CopyXmlNode

        //
        // Copy a single XML node, attempting to preserve whitespace.
        // A side effect of this method is to advance the reader to the next node.
        //
        // PERFORMANCE NOTE: this function is used at runtime to copy a configuration section,
        // and at designtime to copy an entire XML document.
        //
        // At designtime, this function needs to be able to copy a <!DOCTYPE declaration.
        // Copying a <!DOCTYPE declaration is expensive, because due to limitations of the 
        // XmlReader API, we must track the position of the writer to accurately format it. 
        // Tracking the position of the writer is expensive, as it requires examining every
        // character that is written for newline characters, and maintaining the seek position
        // of the underlying stream at each new line, which in turn requires a stream flush.
        //
        // This function must NEVER require tracking the writer position to copy the Xml nodes
        // that are used in a configuration section.
        // 
        internal bool CopyXmlNode(XmlUtilWriter utilWriter) {
            //
            // For nodes that have a closing string, such as "<element  >"
            // the XmlReader API does not give us the location of the closing string, e.g. ">".
            // To correctly determine the location of the closing part, we advance the reader,
            // determine the position of the next node, then work backwards to add whitespace
            // and add the closing string.
            //
            string close = null;
            int lineNumber = -1;
            int linePosition = -1;

            int readerLineNumber = 0;
            int readerLinePosition = 0;
            int writerLineNumber = 0;
            int writerLinePosition = 0;
            if (utilWriter.TrackPosition) {
                readerLineNumber = _reader.LineNumber;
                readerLinePosition = _reader.LinePosition;
                writerLineNumber = utilWriter.LineNumber;
                writerLinePosition = utilWriter.LinePosition;
            }

            // We test the node type in the likely order of decreasing occurrence.
            XmlNodeType nodeType = _reader.NodeType;
            if (nodeType == XmlNodeType.Whitespace) {
                utilWriter.Write(_reader.Value);
            }
            else if (nodeType == XmlNodeType.Element) {
                close = (_reader.IsEmptyElement) ? "/>" : ">";

                // get the line position after the element declaration:
                //      <element    attr="value"
                //              ^
                //              linePosition
                //
                lineNumber = _reader.LineNumber;
                linePosition = _reader.LinePosition + _reader.Name.Length;

                utilWriter.Write('<');
                utilWriter.Write(_reader.Name);

                //
                // Note that there is no way to get spacing between attribute name and value
                // For example:
                //
                //          <elem attr="value" />
                //
                // is reported with the same position as
                //
                //          <elem attr = "value" />
                //
                // The first example has no spaces around '=', the second example does.
                //
                while (_reader.MoveToNextAttribute()) {
                    // get line position of the attribute declaration
                    //      <element attr="value"
                    //               ^
                    //               attrLinePosition
                    //
                    int attrLineNumber = _reader.LineNumber;
                    int attrLinePosition = _reader.LinePosition;

                    // Write the whitespace before the attribute
                    utilWriter.AppendRequiredWhiteSpace(lineNumber, linePosition, attrLineNumber, attrLinePosition);

                    // Write the attribute and value
                    int charactersWritten = utilWriter.Write(_reader.Name);
                    charactersWritten += utilWriter.Write('=');
                    charactersWritten += utilWriter.AppendAttributeValue(_reader);

                    // Update position. Note that the attribute value is escaped to always be on a single line.
                    lineNumber = attrLineNumber;
                    linePosition = attrLinePosition + charactersWritten;
                }
            }
            else if (nodeType == XmlNodeType.EndElement) {
                close = ">";

                // get line position after the end element declaration:
                //      </element    >
                //               ^
                //               linePosition
//.........这里部分代码省略.........
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:101,代码来源:xmlutil.cs

示例2: FormatXmlElement


//.........这里部分代码省略.........
                    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);
                    num = indent;
                    flag3 = false;
                    writer2.Flush();
                    num2 = ((StringWriter) writer2.Writer).GetStringBuilder().Length;
                }
                else
                {
                    flag3 = true;
                }
                reader.MoveToNextAttribute();
                writer2.Flush();
                int length = ((StringWriter) writer2.Writer).GetStringBuilder().Length;
                if (flag3)
                {
                    writer2.AppendSpace();
                }
                writer2.Write(reader.Name);
                writer2.Write('=');
                writer2.AppendAttributeValue(reader);
                writer2.Flush();
                num += ((StringWriter) writer2.Writer).GetStringBuilder().Length - length;
                num4++;
            Label_0274:
                if (num4 < attributeCount)
                {
                    goto Label_01BE;
                }
                reader.MoveToElement();
                if (reader.IsEmptyElement)
                {
                    writer2.Write(" />");
                }
                else
                {
                    writer2.Write('>');
                }
            Label_02D6:
                newLine = true;
                skipFirstIndent = false;
            }
            writer2.Flush();
            return ((StringWriter) writer2.Writer).ToString();
        }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:101,代码来源:XmlUtil.cs

示例3: FormatXmlElement


//.........这里部分代码省略.........
                        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;
                                    writeSpace = false;
                                    utilWriter.Flush();
                                    sbLengthLastNewLine = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                                }
                                else {
                                    writeSpace = true;
                                }

                                // Write the attribute
                                reader.MoveToNextAttribute();
                                utilWriter.Flush();
                                int startLength = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                                if (writeSpace) {
                                    utilWriter.AppendSpace();
                                }

                                utilWriter.Write(reader.Name);
                                utilWriter.Write('=');
                                utilWriter.AppendAttributeValue(reader);
                                utilWriter.Flush();
                                lineWidth += ((StringWriter)utilWriter.Writer).GetStringBuilder().Length - startLength;
                            }
                        }

                        // position reader back on element
                        reader.MoveToElement();

                        // write closing tag
                        if (reader.IsEmptyElement) {
                            utilWriter.Write(" />");
                        }
                        else {
                            utilWriter.Write('>');
                        }

                        break;

                    case XmlNodeType.EndElement:
                        utilWriter.Write("</");
                        utilWriter.Write(reader.Name);
                        utilWriter.Write('>');
                        break;

                    case XmlNodeType.EntityReference:
                        utilWriter.AppendEntityRef(reader.Name);
                        break;

                    // Ignore <?xml and <!DOCTYPE nodes
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.DocumentType:
                    default:
                        break;
                }

                // put each new element on a new line
                newLine = true;

                // do not skip any more indents
                skipFirstIndent = false;
            }

            utilWriter.Flush();
            string s = ((StringWriter)utilWriter.Writer).ToString();
            return s;
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:101,代码来源:xmlutil.cs

示例4: CopyXmlNode

        internal bool CopyXmlNode(XmlUtilWriter utilWriter)
        {
            string s = null;
            int fromLineNumber = -1;
            int fromLinePosition = -1;
            int lineNumber = 0;
            int linePosition = 0;
            int num5 = 0;
            int num6 = 0;
            if (utilWriter.TrackPosition)
            {
                lineNumber = this._reader.LineNumber;
                linePosition = this._reader.LinePosition;
                num5 = utilWriter.LineNumber;
                num6 = utilWriter.LinePosition;
            }
            XmlNodeType nodeType = this._reader.NodeType;
            switch (nodeType)
            {
                case XmlNodeType.Whitespace:
                    utilWriter.Write(this._reader.Value);
                    break;

                case XmlNodeType.Element:
                    s = this._reader.IsEmptyElement ? "/>" : ">";
                    fromLineNumber = this._reader.LineNumber;
                    fromLinePosition = this._reader.LinePosition + this._reader.Name.Length;
                    utilWriter.Write('<');
                    utilWriter.Write(this._reader.Name);
                    while (this._reader.MoveToNextAttribute())
                    {
                        int toLineNumber = this._reader.LineNumber;
                        int toLinePosition = this._reader.LinePosition;
                        utilWriter.AppendRequiredWhiteSpace(fromLineNumber, fromLinePosition, toLineNumber, toLinePosition);
                        int num9 = utilWriter.Write(this._reader.Name) + utilWriter.Write('=');
                        num9 += utilWriter.AppendAttributeValue(this._reader);
                        fromLineNumber = toLineNumber;
                        fromLinePosition = toLinePosition + num9;
                    }
                    break;

                case XmlNodeType.EndElement:
                    s = ">";
                    fromLineNumber = this._reader.LineNumber;
                    fromLinePosition = this._reader.LinePosition + this._reader.Name.Length;
                    utilWriter.Write("</");
                    utilWriter.Write(this._reader.Name);
                    break;

                case XmlNodeType.Comment:
                    utilWriter.AppendComment(this._reader.Value);
                    break;

                case XmlNodeType.Text:
                    utilWriter.AppendEscapeTextString(this._reader.Value);
                    break;

                case XmlNodeType.XmlDeclaration:
                    s = "?>";
                    fromLineNumber = this._reader.LineNumber;
                    fromLinePosition = this._reader.LinePosition + 3;
                    utilWriter.Write("<?xml");
                    while (this._reader.MoveToNextAttribute())
                    {
                        int num10 = this._reader.LineNumber;
                        int num11 = this._reader.LinePosition;
                        utilWriter.AppendRequiredWhiteSpace(fromLineNumber, fromLinePosition, num10, num11);
                        int num12 = utilWriter.Write(this._reader.Name) + utilWriter.Write('=');
                        num12 += utilWriter.AppendAttributeValue(this._reader);
                        fromLineNumber = num10;
                        fromLinePosition = num11 + num12;
                    }
                    this._reader.MoveToElement();
                    break;

                case XmlNodeType.SignificantWhitespace:
                    utilWriter.Write(this._reader.Value);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    utilWriter.AppendProcessingInstruction(this._reader.Name, this._reader.Value);
                    break;

                case XmlNodeType.EntityReference:
                    utilWriter.AppendEntityRef(this._reader.Name);
                    break;

                case XmlNodeType.CDATA:
                    utilWriter.AppendCData(this._reader.Value);
                    break;

                default:
                    if (nodeType == XmlNodeType.DocumentType)
                    {
                        int num13 = utilWriter.Write("<!DOCTYPE");
                        utilWriter.AppendRequiredWhiteSpace(this._lastLineNumber, this._lastLinePosition + num13, this._reader.LineNumber, this._reader.LinePosition);
                        utilWriter.Write(this._reader.Name);
                        string str2 = null;
                        if (this._reader.HasValue)
                        {
//.........这里部分代码省略.........
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:101,代码来源:XmlUtil.cs


注:本文中的System.Configuration.XmlUtilWriter.AppendAttributeValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。