• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Configuration.XmlUtilWriter类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



XmlUtilWriter类属于System.Configuration命名空间,在下文中一共展示了XmlUtilWriter类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: CopyElement

 private void CopyElement(XmlUtilWriter utilWriter)
 {
     int depth = this._reader.Depth;
     bool isEmptyElement = this._reader.IsEmptyElement;
     this.CopyXmlNode(utilWriter);
     while (this._reader.Depth > depth)
     {
         this.CopyXmlNode(utilWriter);
     }
     if (!isEmptyElement)
     {
         this.CopyXmlNode(utilWriter);
     }
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:14,代码来源:XmlUtil.cs


示例2: SaveConfigSource

        //
        // SaveConfigSource
        //
        private void SaveConfigSource(DefinitionUpdate update) {

            string configSourceStreamName;

            if (update.SectionRecord.HasResult) {
                ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
                configSourceStreamName = configSection.SectionInformation.ConfigSourceStreamName;
            }
            else {
                Debug.Assert(update.SectionRecord.HasFileInput, "update.SectionRecord.HasFileInput");
                SectionInput fileInput = update.SectionRecord.FileInput;
                configSourceStreamName = fileInput.SectionXmlInfo.ConfigSourceStreamName;
            }

            // Copy the input stream before opening the output stream.
            byte[] readBuffer = null;
            using (Stream streamRead = Host.OpenStreamForRead(configSourceStreamName)) {
                if (streamRead != null) {
                    readBuffer = new byte[streamRead.Length];
                    int count = streamRead.Read(readBuffer, 0, (int) streamRead.Length);
                    if (count != streamRead.Length) {
                        throw new ConfigurationErrorsException();
                    }
                }
            }

            // Write the changes to the output stream.
            bool hasFile = (readBuffer != null);
            object writeContext = null;
            bool streamOpened = false;

            try {
                try {
                    string templateStreamName;

                    if (Host.IsRemote) {
                        // templateStreamName is used by OpenStreamForWrite for copying file attributes during saving.
                        // (for details, see WriteFileContext.Complete.)
                        //
                        // If we're using a remote host, then ConfigStreamInfo.StreamName is actually pointing to a
                        // full filepath on a remote machine.  In this case, it's impossible to copy the attributes
                        // over, and thus we won't do it.
                        templateStreamName = null;
                    }
                    else {
                        templateStreamName = ConfigStreamInfo.StreamName;
                    }

                    using (Stream streamWrite = Host.OpenStreamForWrite(configSourceStreamName, templateStreamName, ref writeContext)) {
                        streamOpened = true;
                        if (update.UpdatedXml == null) {
                            Debug.Assert(hasFile, "hasFile");
                            if (hasFile) {
                                streamWrite.Write(readBuffer, 0, readBuffer.Length);
                            }
                        }
                        else {
                            using (StreamWriter streamWriter = new StreamWriter(streamWrite)) {
                                XmlUtilWriter utilWriter = new XmlUtilWriter(streamWriter, true);
                                if (hasFile) {
                                    CopyConfigSource(utilWriter, update.UpdatedXml, configSourceStreamName, readBuffer);
                                }
                                else {
                                    CreateNewConfigSource(utilWriter, update.UpdatedXml, DEFAULT_INDENT);
                                }
                            }
                        }
                    }
                }
                catch {
                    if (streamOpened) {
                        Host.WriteCompleted(configSourceStreamName, false, writeContext);
                    }

                    throw;
                }
            }
            //
            // Guarantee that exceptions contain at least the name of the stream by wrapping them
            // in a ConfigurationException.
            //
            catch (Exception e) {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, configSourceStreamName, 0);
            }

            Host.WriteCompleted(configSourceStreamName, true, writeContext);
        }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:90,代码来源:MgmtConfigurationRecord.cs


示例3: CreateNewConfigSource

 private void CreateNewConfigSource(XmlUtilWriter utilWriter, string updatedXml, int indent) {
     string formattedXml = XmlUtil.FormatXmlElement(updatedXml, 0, indent, true);
     utilWriter.Write(string.Format(CultureInfo.InvariantCulture, FORMAT_CONFIGSOURCE_FILE, ConfigStreamInfo.StreamEncoding.WebName));
     utilWriter.Write(formattedXml + NL);
 }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:5,代码来源:MgmtConfigurationRecord.cs


示例4: CopyConfig

        // Copy a config file, replacing sections with updates.
        private void CopyConfig(SectionUpdates declarationUpdates, ConfigDefinitionUpdates definitionUpdates,
                byte[] buffer, string filename, NamespaceChange namespaceChange, XmlUtilWriter utilWriter) {

            CheckPreamble(ConfigStreamInfo.StreamEncoding.GetPreamble(), utilWriter, buffer);

            using (Stream stream = new MemoryStream(buffer)) {
                using (XmlUtil xmlUtil = new XmlUtil(stream, filename, false)) {
                    // copy up to the <configuration> node
                    XmlTextReader reader = xmlUtil.Reader;
                    reader.WhitespaceHandling = WhitespaceHandling.All;
                    reader.Read();
                    xmlUtil.CopyReaderToNextElement(utilWriter, false);

                    Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == KEYWORD_CONFIGURATION,
                                 "reader.NodeType == XmlNodeType.Element && reader.Name == KEYWORD_CONFIGURATION");

                    int indent = DEFAULT_INDENT;
                    int configurationElementLinePosition = xmlUtil.TrueLinePosition;
                    bool isEmptyConfigurationElement = reader.IsEmptyElement;

                    // copy <configuration> node
                    // if the node is an empty element, we may need to open it.
                    string configurationStartElement;
                    if (namespaceChange == NamespaceChange.Add) {
                        configurationStartElement = string.Format(
                            CultureInfo.InvariantCulture, FORMAT_CONFIGURATION_NAMESPACE, KEYWORD_CONFIGURATION_NAMESPACE);
                    }
                    else if (namespaceChange == NamespaceChange.Remove) {
                        configurationStartElement = FORMAT_CONFIGURATION;
                    }
                    else {
                        configurationStartElement = null;
                    }

                    bool needsChildren = (declarationUpdates != null || definitionUpdates  != null);
                    string configurationEndElement = xmlUtil.UpdateStartElement(utilWriter, configurationStartElement, needsChildren, configurationElementLinePosition, indent);

                    bool foundConfigSectionsElement = false;
                    if (!isEmptyConfigurationElement) {
                        // copy up to the first element under <configuration>
                        xmlUtil.CopyReaderToNextElement(utilWriter, true);

                        // updateIndent
                        indent = UpdateIndent(indent, xmlUtil, utilWriter, configurationElementLinePosition);

                        if (reader.NodeType == XmlNodeType.Element && reader.Name == KEYWORD_CONFIGSECTIONS) {
                            foundConfigSectionsElement = true;

                            int configSectionsElementLinePosition = xmlUtil.TrueLinePosition;
                            bool isEmptyConfigSectionsElement = reader.IsEmptyElement;

                            // if no updates, copy the entire <configSections> element
                            if (declarationUpdates == null) {
                                xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
                            }
                            else {
                                // copy <configSections>, and open it if it is an empty element
                                string configSectionsEndElement = xmlUtil.UpdateStartElement(
                                    utilWriter, null, true, configSectionsElementLinePosition, indent);

                                if (!isEmptyConfigSectionsElement) {
                                    // copy to next element under <configSections>, or up to closing </configSections>
                                    xmlUtil.CopyReaderToNextElement(utilWriter, true);

                                    // copy config declarations
                                    CopyConfigDeclarationsRecursive(declarationUpdates, xmlUtil, utilWriter, string.Empty,
                                            configSectionsElementLinePosition, indent);

                                    Debug.Assert(reader.NodeType == XmlNodeType.EndElement && reader.Name == KEYWORD_CONFIGSECTIONS,
                                                 "reader.NodeType == XmlNodeType.EndElement && reader.Name == \"KEYWORD_CONFIGSECTIONS\"");
                                }

                                // write declarations not written by above copy
                                if (declarationUpdates.HasUnretrievedSections()) {

                                    // determine the line position of the end element
                                    int endElementLinePosition = 0;
                                    if (configSectionsEndElement == null) {
                                        endElementLinePosition = xmlUtil.TrueLinePosition;
                                    }

                                    // indent a new line
                                    if (!utilWriter.IsLastLineBlank) {
                                        utilWriter.AppendNewLine();
                                    }

                                    WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, configSectionsElementLinePosition + indent, indent, false);

                                    // restore spaces to end element
                                    if (configSectionsEndElement == null) {
                                        utilWriter.AppendSpacesToLinePosition(endElementLinePosition);
                                    }
                                }

                                // Copy the </configSections> element
                                if (configSectionsEndElement == null) {
                                    xmlUtil.CopyXmlNode(utilWriter);
                                }
                                else {
//.........这里部分代码省略.........
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:101,代码来源:MgmtConfigurationRecord.cs


示例5: CopyConfigDefinitionsRecursive

        // Copy configuration sections from the original configuration file.
        private bool CopyConfigDefinitionsRecursive(
                ConfigDefinitionUpdates configDefinitionUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter,
                bool locationPathApplies, LocationUpdates locationUpdates, SectionUpdates sectionUpdates,
                bool addNewSections, string group, int parentLinePosition, int parentIndent) {

            bool wroteASection = false;
            XmlTextReader reader = xmlUtil.Reader;
            int linePosition;
            int indent;
            int startingLinePosition;

            indent = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);

            if (reader.NodeType == XmlNodeType.Element) {
                linePosition = xmlUtil.TrueLinePosition;
                startingLinePosition = linePosition;
            }
            else if (reader.NodeType == XmlNodeType.EndElement) {
                linePosition = parentLinePosition + indent;
                if (utilWriter.IsLastLineBlank) {
                    startingLinePosition = xmlUtil.TrueLinePosition;
                }
                else {
                    startingLinePosition = parentLinePosition;
                }
            }
            else {
                linePosition = parentLinePosition + indent;
                startingLinePosition = 0;
            }

            //
            // Write any new sections that apply to this group
            //
            if (sectionUpdates != null && addNewSections) {
                // Remove newness, so we won't write again
                sectionUpdates.IsNew = false;

                Debug.Assert(locationPathApplies, "locationPathApplies");
                string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
                if (movedSectionNames != null) {
                    if (!utilWriter.IsLastLineBlank) {
                        utilWriter.AppendNewLine();
                    }

                    utilWriter.AppendSpacesToLinePosition(linePosition);
                    bool skipFirstIndent = true;

                    foreach (string configKey in movedSectionNames) {
                        DefinitionUpdate update = sectionUpdates.GetDefinitionUpdate(configKey);

                        WriteSectionUpdate(utilWriter, update, linePosition, indent, skipFirstIndent);
                        skipFirstIndent = false;
                        utilWriter.AppendNewLine();
                        wroteASection = true;
                    }

                    // Restore the whitespace we used for the first element, which is either a start or an end element.
                    utilWriter.AppendSpacesToLinePosition(startingLinePosition);
                }
            }

            if (reader.NodeType == XmlNodeType.Element) {
                //
                // For each element at this depth, either:
                // - Write the element verbatim and recurse due to a location section or group hierarchy element.
                // - Write the element verbatim because it is unchanged, or because the current location does
                //   not apply.
                // - Write the updated XML for the section.
                // - Skip it because the section has been removed.
                //
                int depth = reader.Depth;
                while (reader.Depth == depth) {
                    bool                recurse = false;
                    DefinitionUpdate    update = null;
                    bool                elementLocationPathApplies = locationPathApplies;
                    LocationUpdates     recurseLocationUpdates = locationUpdates;
                    SectionUpdates      recurseSectionUpdates = sectionUpdates;
                    bool                recurseAddNewSections = addNewSections;
                    string              recurseGroup = group;
                    bool                removedSectionOrGroup = false;

                    // update the lineposition and indent for each element
                    indent = UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
                    linePosition = xmlUtil.TrueLinePosition;

                    string elementName = reader.Name;
                    if (elementName == KEYWORD_LOCATION) {
                        string locationSubPathAttribute = reader.GetAttribute(KEYWORD_LOCATION_PATH);
                        locationSubPathAttribute = NormalizeLocationSubPath(locationSubPathAttribute, xmlUtil);
                        elementLocationPathApplies = false;
                        OverrideModeSetting overrideMode = OverrideModeSetting.LocationDefault;
                        bool inheritInChildApps = true;

                        if (IsLocationConfig) {
                            // For location config we will compare config paths instead of location strings
                            // so that we dont end up comparing "1" with "Default Web Site" and ending up with the wrong result
                            if (locationSubPathAttribute == null) {
                                elementLocationPathApplies = false;
//.........这里部分代码省略.........
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:101,代码来源:MgmtConfigurationRecord.cs


示例6: WriteNewConfigDefinitions

        private void WriteNewConfigDefinitions(ConfigDefinitionUpdates configDefinitionUpdates, XmlUtilWriter utilWriter, int linePosition, int indent) {
            if (configDefinitionUpdates == null)
                return;

            foreach (LocationUpdates locationUpdates in configDefinitionUpdates.LocationUpdatesList) {
                SectionUpdates sectionUpdates = locationUpdates.SectionUpdates;
                if (sectionUpdates.IsEmpty || !sectionUpdates.IsNew)
                    continue;

                configDefinitionUpdates.FlagLocationWritten();
                bool writeLocationTag = _locationSubPath != null || !locationUpdates.IsDefault;
                int recurseLinePosition = linePosition;

                utilWriter.AppendSpacesToLinePosition(linePosition);

                if (writeLocationTag) {
                    // write the <location> start tag
                    if (_locationSubPath == null) {
                        utilWriter.Write(String.Format(CultureInfo.InvariantCulture, FORMAT_LOCATION_NOPATH, locationUpdates.OverrideMode.LocationTagXmlString, BoolToString(locationUpdates.InheritInChildApps)));
                    }
                    else {
                        utilWriter.Write(String.Format(CultureInfo.InvariantCulture, FORMAT_LOCATION_PATH, locationUpdates.OverrideMode.LocationTagXmlString, BoolToString(locationUpdates.InheritInChildApps), _locationSubPath));
                    }

                    recurseLinePosition += indent;
                    utilWriter.AppendSpacesToLinePosition(recurseLinePosition);
                }

                // Invoke the recursive write.
                WriteNewConfigDefinitionsRecursive(utilWriter, locationUpdates.SectionUpdates, recurseLinePosition, indent, true);

                if (writeLocationTag) {
                    // Write the location end tag
                    utilWriter.AppendSpacesToLinePosition(linePosition);
                    utilWriter.Write(FORMAT_LOCATION_ENDELEMENT);
                    utilWriter.AppendNewLine();
                }
            }

            if (configDefinitionUpdates.RequireLocation) {
                Debug.Assert(IsLocationConfig, "IsLocationConfig");

                // If we still require this to be written, then we must write it out now
                configDefinitionUpdates.FlagLocationWritten();

                utilWriter.AppendSpacesToLinePosition(linePosition);

                utilWriter.Write(String.Format(CultureInfo.InvariantCulture, FORMAT_LOCATION_PATH, OverrideModeSetting.LocationDefault.LocationTagXmlString, KEYWORD_TRUE, _locationSubPath));
                utilWriter.AppendSpacesToLinePosition(linePosition);
                utilWriter.Write(FORMAT_LOCATION_ENDELEMENT);
                utilWriter.AppendNewLine();
            }
        }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:53,代码来源:MgmtConfigurationRecord.cs


示例7: CheckPreamble

        private void CheckPreamble(byte[] preamble, XmlUtilWriter utilWriter, byte[] buffer) {
            bool hasByteOrderMark = false;
            using (Stream preambleStream = new MemoryStream(buffer)) {
                byte[] streamStart = new byte[preamble.Length];
                if (preambleStream.Read(streamStart, 0, streamStart.Length) == streamStart.Length) {
                    hasByteOrderMark = true;
                    for (int i = 0; i < streamStart.Length; i++) {
                        if (streamStart[i] != preamble[i]) {
                            hasByteOrderMark = false;
                            break;
                        }
                    }
                }
            }

            if (!hasByteOrderMark) {
                // Force the writer to emit byte order mark, then reset the stream
                // so that it is written over.
                object checkpoint = utilWriter.CreateStreamCheckpoint();
                utilWriter.Write('x');
                utilWriter.RestoreStreamCheckpoint(checkpoint);
            }
        }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:23,代码来源:MgmtConfigurationRecord.cs


示例8: CopyReaderToNextElement

        //
        // Copy the reader until we hit an element, or we've exited the current depth.
        //
        internal bool CopyReaderToNextElement(XmlUtilWriter utilWriter, bool limitDepth) {
            bool moreToRead = true;

            // Set the depth if we limit copying to this depth
            int depth;
            if (limitDepth) {
                // there is nothing in the element
                if (_reader.NodeType == XmlNodeType.EndElement)
                    return true;

                depth = _reader.Depth;
            }
            else {
                depth = 0;
            }

            // Copy nodes until we've reached the desired depth, or until we hit an element.
            do {
                if (_reader.NodeType == XmlNodeType.Element)
                    break;

                if (_reader.Depth < depth) {
                    break;
                }

                moreToRead = CopyXmlNode(utilWriter);
            } while (moreToRead);

            return moreToRead;
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:33,代码来源:xmlutil.cs


示例9: SkipAndCopyReaderToNextElement

        //
        // Skip over the current element and copy until the next element.
        // This function removes the one blank line that would otherwise
        // be inserted by simply skipping and copying to the next element
        // in a situation like this:
        //
        //      <!-- end of previous configSection -->
        //      <configSectionToDelete>
        //          <content />
        //          <moreContent />
        //      </configSectionToDelete>
        //      <!-- end of configSectionToDelete -->
        //      <nextConfigSection />
        //
        internal bool SkipAndCopyReaderToNextElement(XmlUtilWriter utilWriter, bool limitDepth) {
            Debug.Assert(_reader.NodeType == XmlNodeType.Element, "_reader.NodeType == XmlNodeType.Element");

            // If the last line before the element is not blank, then we do not have to
            // remove the blank line.
            if (!utilWriter.IsLastLineBlank) {
                _reader.Skip();
                return CopyReaderToNextElement(utilWriter, limitDepth);
            }

            // Set the depth if we limit copying to this depth
            int depth;
            if (limitDepth) {
                depth = _reader.Depth;
            }
            else {
                depth = 0;
            }

            // Skip over the element
            _reader.Skip();

            int lineNumberOfEndElement = _reader.LineNumber;

            // Read until we hit a a non-whitespace node or reach the end
            while (!_reader.EOF) {
                if (_reader.NodeType != XmlNodeType.Whitespace) {
                    //
                    // If the next non-whitepace node is on another line,
                    // seek back to the beginning of the current blank line,
                    // skip a blank line of whitespace, and copy the remaining whitespace.
                    //
                    if (_reader.LineNumber > lineNumberOfEndElement) {
                        utilWriter.SeekToLineStart();
                        utilWriter.AppendWhiteSpace(lineNumberOfEndElement + 1, 1, LineNumber, TrueLinePosition);
                    }

                    break;
                }

                _reader.Read();
            }

            // Copy nodes until we've reached the desired depth, or until we hit an element.
            while (!_reader.EOF) {
                if (_reader.NodeType == XmlNodeType.Element)
                    break;

                if (_reader.Depth < depth) {
                    break;
                }

                CopyXmlNode(utilWriter);
            };

            return !_reader.EOF;
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:71,代码来源:xmlutil.cs


示例10: CopyOuterXmlToNextElement

        //
        // Copy an XML element, then continue copying until we've hit the next element
        // or exited this depth.
        //
        internal bool CopyOuterXmlToNextElement(XmlUtilWriter utilWriter, bool limitDepth) {
            CopyElement(utilWriter);

            // Copy until reaching the next element, or if limitDepth == true until we've exited this depth.
            return CopyReaderToNextElement(utilWriter, limitDepth);
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:10,代码来源:xmlutil.cs


示例11: SkipChildElementsAndCopyOuterXmlToNextElement

        //
        // Copy an XML element but skip all its child elements, then continue copying until we've hit the next element.
        //
        internal bool SkipChildElementsAndCopyOuterXmlToNextElement(XmlUtilWriter utilWriter) {
            bool    isEmptyElement = _reader.IsEmptyElement;
            int     startingLine = _reader.LineNumber;
#if DBG
            int     depth = _reader.Depth;
#endif

            Debug.Assert(_reader.NodeType == XmlNodeType.Element, "_reader.NodeType == XmlNodeType.Element");

            CopyXmlNode(utilWriter);

            // See if we need to skip any child element
            if (!isEmptyElement) {
                while (_reader.NodeType != XmlNodeType.EndElement) {
                    
                    // Skip all the inner child elements
                    if (_reader.NodeType == XmlNodeType.Element) {
                        _reader.Skip();

                        // We need to skip all the whitespaces following a skipped element.
                        // - If the whitespaces don't contain /r/n, then it's okay to skip them 
                        //   as part of the element.
                        // - If the whitespaces contain /r/n, not skipping them will result
                        //   in a redundant emtpy line being copied.
                        if (_reader.NodeType == XmlNodeType.Whitespace) {
                            _reader.Skip();
                        }
                    }
                    else {
                        // We want to preserve other content, e.g. comments.
                        CopyXmlNode(utilWriter);
                    }
                }

                if (_reader.LineNumber != startingLine) {
                    // The whitespace in front of the EndElement was skipped above.
                    // We need to append spaces to compensate for that.
                    utilWriter.AppendSpacesToLinePosition(TrueLinePosition);
                }


#if DBG                
                Debug.Assert(_reader.Depth == depth, "We should be at the same depth as the opening Element");
#endif
                    
                // Copy the end element.
                CopyXmlNode(utilWriter);
            }                

            return CopyReaderToNextElement(utilWriter, true);
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:54,代码来源:xmlutil.cs


示例12: 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;
//.........这里部分代码省略.........
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:101,代码来源:xmlutil.cs


示例13: CreateNewConfigSource

 private void CreateNewConfigSource(XmlUtilWriter utilWriter, string updatedXml, int indent)
 {
     string formattedXml = XmlUtil.FormatXmlElement(updatedXml, 0, indent, true);
     utilWriter.Write(string.Format(CultureInfo.InvariantCulture, FormatConfigsourceFile,
         ConfigStreamInfo.StreamEncoding.WebName));
     utilWriter.Write(formattedXml + NewLine);
 }
开发者ID:chcosta,项目名称:corefx,代码行数:7,代码来源:MgmtConfigurationRecord.cs


示例14: CreateNewConfig

        // Create a new config file.
        private void CreateNewConfig(
            SectionUpdates declarationUpdates,
            ConfigDefinitionUpdates definitionUpdates,
            NamespaceChange namespaceChange,
            XmlUtilWriter utilWriter)
        {
            // Write Header
            utilWriter.Write(string.Format(CultureInfo.InvariantCulture,
                FormatNewconfigfile,
                ConfigStreamInfo.StreamEncoding.WebName));

            // Write <configuration> tag
            if (namespaceChange == NamespaceChange.Add)
            {
                utilWriter.Write(string.Format(CultureInfo.InvariantCulture,
                    FormatConfigurationNamespace,
                    KeywordConfigurationNamespace));
            }
            else utilWriter.Write(FormatConfiguration);

            const int LineIndent = DefaultIndent + 1;

            if (declarationUpdates != null)
                WriteNewConfigDeclarations(declarationUpdates, utilWriter, LineIndent, DefaultIndent, false);

            WriteNewConfigDefinitions(definitionUpdates, utilWriter, LineIndent, DefaultIndent);

            utilWriter.Write(FormatConfigurationEndelement);
        }
开发者ID:chcosta,项目名称:corefx,代码行数:30,代码来源:MgmtConfigurationRecord.cs


示例15: WriteUnwrittenConfigDeclarations

 private void WriteUnwrittenConfigDeclarations(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
     WriteUnwrittenConfigDeclarationsRecursive(declarationUpdates, utilWriter, linePosition, indent, skipFirstIndent);
 }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:3,代码来源:MgmtConfigurationRecord.cs


示例16: CopyElement

        //
        // Copy an XML element and its children, up to and including the end element.
        //
        private void CopyElement(XmlUtilWriter utilWriter) {
            Debug.Assert(_reader.NodeType== XmlNodeType.Element, "_reader.NodeType== XmlNodeType.Element");

            int depth = _reader.Depth;
            bool isEmptyElement = _reader.IsEmptyElement;

            // Copy current node
            CopyXmlNode(utilWriter);

            // Copy nodes while the depth is greater than the current depth.
            while (_reader.Depth > depth) {
                CopyXmlNode(utilWriter);
            }

            // Copy the end element.
            if (!isEmptyElement) {
                CopyXmlNode(utilWriter);
            }
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:22,代码来源:xmlutil.cs


示例17: WriteUnwrittenConfigDeclarationsRecursive

        private void WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
            string[] unretrievedSectionNames = declarationUpdates.GetUnretrievedSectionNames();
            if (unretrievedSectionNames != null) {
                foreach (string configKey in unretrievedSectionNames) {
                    Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
                    if (!skipFirstIndent) {
                        utilWriter.AppendSpacesToLinePosition(linePosition);
                    }
                    skipFirstIndent = false;

                    DeclarationUpdate update = declarationUpdates.GetDeclarationUpdate(configKey);
                    if (update != null && !string.IsNullOrEmpty(update.UpdatedXml)) {
                        utilWriter.Write(update.UpdatedXml);
                        utilWriter.AppendNewLine();
                    }
                }
            }

            string[] unretrievedGroupNames = declarationUpdates.GetUnretrievedGroupNames();
            if (unretrievedGroupNames != null) {
                foreach (string group in unretrievedGroupNames) {
                    if (TargetFramework != null) {
                        ConfigurationSectionGroup g = GetSectionGroup(group);
                        if (g != null && !g.ShouldSerializeSectionGroupInTargetVersion(TargetFramework)){
                            declarationUpdates.MarkGroupAsRetrieved(group);
                            continue;
                        }
                    }
                    if (!skipFirstIndent) {
                        utilWriter.AppendSpacesToLinePosition(linePosition);
                    }
                    skipFirstIndent = false;

                    SectionUpdates declarationUpdatesChild = declarationUpdates.GetSectionUpdatesForGroup(group);
                    DeclarationUpdate groupUpdate = declarationUpdatesChild.GetSectionGroupUpdate();
                    if (groupUpdate == null) {
                        utilWriter.Write("<sectionGroup name=\"" + group + "\">");
                    }
                    else {
                        utilWriter.Write(groupUpdate.UpdatedXml);
                    }
                    utilWriter.AppendNewLine();

                    WriteUnwrittenConfigDeclarationsRecursive(declarationUpdatesChild, utilWriter, linePosition + indent, indent, false);
                    utilWriter.AppendSpacesToLinePosition(linePosition);
                    utilWriter.Write("</sectionGroup>\r\n");
                }
            }
        }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:49,代码来源:MgmtConfigurationRecord.cs


示例18: 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) 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Install.AssemblyInstaller类代码示例发布时间:2022-05-26
下一篇:
C# Configuration.SettingsPropertyValueCollection类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap