本文整理汇总了C#中System.Configuration.XmlUtilWriter.Write方法的典型用法代码示例。如果您正苦于以下问题:C# XmlUtilWriter.Write方法的具体用法?C# XmlUtilWriter.Write怎么用?C# XmlUtilWriter.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Configuration.XmlUtilWriter
的用法示例。
在下文中一共展示了XmlUtilWriter.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyConfigSource
private void CopyConfigSource(XmlUtilWriter utilWriter, string updatedXml, string configSourceStreamName, byte[] buffer) {
// only copy the byte order mark if it exists in the current web.config
byte[] preamble;
using (Stream stream = new MemoryStream(buffer)) {
using (XmlUtil xmlUtil = new XmlUtil(stream, configSourceStreamName, true)) {
preamble = ConfigStreamInfo.StreamEncoding.GetPreamble();
}
}
CheckPreamble(preamble, utilWriter, buffer);
using (Stream stream = new MemoryStream(buffer)) {
using (XmlUtil xmlUtil = new XmlUtil(stream, configSourceStreamName, false)) {
XmlTextReader reader = xmlUtil.Reader;
// copy up to the first element
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.Read();
// determine the indent to use for the element
int indent = DEFAULT_INDENT;
int linePosition = 1;
bool hasElement = xmlUtil.CopyReaderToNextElement(utilWriter, false);
if (hasElement) {
// find the indent of the first attribute, if any
int lineNumber = reader.LineNumber;
linePosition = reader.LinePosition - 1;
int attributeIndent = 0;
while (reader.MoveToNextAttribute()) {
if (reader.LineNumber > lineNumber) {
attributeIndent = reader.LinePosition - linePosition;
break;
}
}
// find the indent of the first sub element, if any
int elementIndent = 0;
reader.Read();
while (reader.Depth >= 1) {
if (reader.NodeType == XmlNodeType.Element) {
elementIndent = (reader.LinePosition - 1) - linePosition;
break;
}
reader.Read();
}
if (elementIndent > 0) {
indent = elementIndent;
}
else if (attributeIndent > 0) {
indent = attributeIndent;
}
}
// Write the config source
string formattedXml = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, true);
utilWriter.Write(formattedXml);
// Copy remaining contents
if (hasElement) {
// Skip over the existing element
while (reader.Depth > 0) {
reader.Read();
}
if (reader.IsEmptyElement || reader.NodeType == XmlNodeType.EndElement) {
reader.Read();
}
// Copy remainder of file
while (xmlUtil.CopyXmlNode(utilWriter)) {
}
}
}
}
}
示例2: WriteNewConfigDefinitionsRecursive
private bool WriteNewConfigDefinitionsRecursive(XmlUtilWriter utilWriter, SectionUpdates sectionUpdates, int linePosition, int indent, bool skipFirstIndent)
{
bool flag = false;
string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
if (movedSectionNames != null)
{
flag = true;
foreach (string str in movedSectionNames)
{
DefinitionUpdate definitionUpdate = sectionUpdates.GetDefinitionUpdate(str);
this.WriteSectionUpdate(utilWriter, definitionUpdate, linePosition, indent, skipFirstIndent);
utilWriter.AppendNewLine();
skipFirstIndent = false;
}
}
string[] newGroupNames = sectionUpdates.GetNewGroupNames();
if (newGroupNames != null)
{
foreach (string str2 in newGroupNames)
{
if (base.TargetFramework != null)
{
ConfigurationSectionGroup sectionGroup = this.GetSectionGroup(str2);
if ((sectionGroup != null) && !sectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework))
{
sectionUpdates.MarkGroupAsRetrieved(str2);
continue;
}
}
if (!skipFirstIndent)
{
utilWriter.AppendSpacesToLinePosition(linePosition);
}
skipFirstIndent = false;
utilWriter.Write("<" + str2 + ">\r\n");
if (this.WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates.GetSectionUpdatesForGroup(str2), linePosition + indent, indent, false))
{
flag = true;
}
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write("</" + str2 + ">\r\n");
}
}
sectionUpdates.IsNew = false;
return flag;
}
示例3: 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);
}
}
示例4: CopyConfigDeclarationsRecursive
private bool CopyConfigDeclarationsRecursive(
SectionUpdates declarationUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, 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 section declarations that apply to this group
//
if (declarationUpdates != null) {
string[] movedSectionNames = declarationUpdates.GetMovedSectionNames();
if (movedSectionNames != null) {
if (!utilWriter.IsLastLineBlank) {
utilWriter.AppendNewLine();
}
foreach (string configKey in movedSectionNames) {
DeclarationUpdate sectionUpdate = declarationUpdates.GetDeclarationUpdate(configKey);
Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
// Write the one line section declaration.
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(sectionUpdate.UpdatedXml);
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 group hierarchy element.
// - Write the element verbatim because it is unchanged
// - 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;
DeclarationUpdate sectionUpdate = null;
DeclarationUpdate groupUpdate = null;
SectionUpdates declarationUpdatesChild = null;
SectionUpdates recurseDeclarationUpdates = declarationUpdates;
string recurseGroup = group;
// update the lineposition and indent for each element
indent = UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
linePosition = xmlUtil.TrueLinePosition;
string directive = reader.Name;
string name = reader.GetAttribute(KEYWORD_SECTIONGROUP_NAME);
string configKey = CombineConfigKey(group, name);
if (directive == KEYWORD_SECTIONGROUP) {
// it's a group - get the updates for children
declarationUpdatesChild = declarationUpdates.GetSectionUpdatesForGroup(name);
if (declarationUpdatesChild != null) {
// get the group update
groupUpdate = declarationUpdatesChild.GetSectionGroupUpdate();
// recurse if there are more sections to copy
if (declarationUpdatesChild.HasUnretrievedSections()) {
recurse = true;
recurseGroup = configKey;
recurseDeclarationUpdates = declarationUpdatesChild;
}
}
}
else {
// it is a section - get the update
Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
sectionUpdate = declarationUpdates.GetDeclarationUpdate(configKey);
//.........这里部分代码省略.........
示例5: WriteNewConfigDeclarations
private void WriteNewConfigDeclarations(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
if (!skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
utilWriter.Write("<configSections>\r\n");
WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, linePosition + indent, indent, false);
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write("</configSections>\r\n");
if (skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
}
示例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();
}
}
示例7: CopyConfigSource
private void CopyConfigSource(XmlUtilWriter utilWriter, string updatedXml, string configSourceStreamName, byte[] buffer)
{
byte[] preamble;
using (Stream stream = new MemoryStream(buffer))
{
using (new XmlUtil(stream, configSourceStreamName, true))
{
preamble = base.ConfigStreamInfo.StreamEncoding.GetPreamble();
}
}
this.CheckPreamble(preamble, utilWriter, buffer);
using (Stream stream2 = new MemoryStream(buffer))
{
using (XmlUtil util2 = new XmlUtil(stream2, configSourceStreamName, false))
{
XmlTextReader reader = util2.Reader;
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.Read();
int indent = 4;
int linePosition = 1;
bool flag = util2.CopyReaderToNextElement(utilWriter, false);
if (flag)
{
int lineNumber = reader.LineNumber;
linePosition = reader.LinePosition - 1;
int num4 = 0;
while (reader.MoveToNextAttribute())
{
if (reader.LineNumber > lineNumber)
{
num4 = reader.LinePosition - linePosition;
break;
}
}
int num5 = 0;
reader.Read();
while (reader.Depth >= 1)
{
if (reader.NodeType == XmlNodeType.Element)
{
num5 = (reader.LinePosition - 1) - linePosition;
break;
}
reader.Read();
}
if (num5 > 0)
{
indent = num5;
}
else if (num4 > 0)
{
indent = num4;
}
}
string s = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, true);
utilWriter.Write(s);
if (flag)
{
while (reader.Depth > 0)
{
reader.Read();
}
if (reader.IsEmptyElement || (reader.NodeType == XmlNodeType.EndElement))
{
reader.Read();
}
while (util2.CopyXmlNode(utilWriter))
{
}
}
}
}
}
示例8: UpdateStartElement
//
// Copy or replace an element node.
// If the element is an empty element, replace it with a formatted start element if either:
// * The contents of the start element string need updating.
// * The element needs to contain child elements.
//
// If the element is empty and is replaced with a start/end element pair, return a
// end element string with whitespace formatting; otherwise return null.
//
internal string UpdateStartElement(XmlUtilWriter utilWriter, string updatedStartElement, bool needsChildren, int linePosition, int indent) {
Debug.Assert(_reader.NodeType == XmlNodeType.Element, "_reader.NodeType == NodeType.Element");
string endElement = null;
bool needsEndElement = false;
string elementName;
elementName = _reader.Name;
// If the element is empty, determine if a new end element is needed.
if (_reader.IsEmptyElement) {
if (updatedStartElement == null && needsChildren) {
updatedStartElement = RetrieveFullOpenElementTag();
}
needsEndElement = (updatedStartElement != null);
}
if (updatedStartElement == null) {
//
// If no changes to the start element are required, just copy it.
//
CopyXmlNode(utilWriter);
}
else {
//
// Format a new start element/end element pair
//
string updatedEndElement = "</" + elementName + ">";
string updatedElement = updatedStartElement + updatedEndElement;
string formattedElement = FormatXmlElement(updatedElement, linePosition, indent, true);
//
// Get the start and end element strings from the formatted element.
//
int iEndElement = formattedElement.LastIndexOf('\n') + 1;
string startElement;
if (needsEndElement) {
endElement = formattedElement.Substring(iEndElement);
// Include a newline in the start element as we are expanding an empty element.
startElement = formattedElement.Substring(0, iEndElement);
}
else {
// Omit the newline from the start element.
startElement = formattedElement.Substring(0, iEndElement - 2);
}
// Write the new start element.
utilWriter.Write(startElement);
// Skip over the existing start element.
_reader.Read();
}
return endElement;
}
示例9: CopyConfig
private void CopyConfig(SectionUpdates declarationUpdates, ConfigDefinitionUpdates definitionUpdates, byte[] buffer, string filename, NamespaceChange namespaceChange, XmlUtilWriter utilWriter)
{
this.CheckPreamble(base.ConfigStreamInfo.StreamEncoding.GetPreamble(), utilWriter, buffer);
using (Stream stream = new MemoryStream(buffer))
{
using (XmlUtil util = new XmlUtil(stream, filename, false))
{
string str;
XmlTextReader reader = util.Reader;
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.Read();
util.CopyReaderToNextElement(utilWriter, false);
int indent = 4;
int trueLinePosition = util.TrueLinePosition;
bool isEmptyElement = reader.IsEmptyElement;
if (namespaceChange == NamespaceChange.Add)
{
str = string.Format(CultureInfo.InvariantCulture, "<configuration xmlns=\"{0}\">\r\n", new object[] { "http://schemas.microsoft.com/.NetConfiguration/v2.0" });
}
else if (namespaceChange == NamespaceChange.Remove)
{
str = "<configuration>\r\n";
}
else
{
str = null;
}
bool needsChildren = (declarationUpdates != null) || (definitionUpdates != null);
string s = util.UpdateStartElement(utilWriter, str, needsChildren, trueLinePosition, indent);
bool flag3 = false;
if (!isEmptyElement)
{
util.CopyReaderToNextElement(utilWriter, true);
indent = this.UpdateIndent(indent, util, utilWriter, trueLinePosition);
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "configSections"))
{
flag3 = true;
int linePosition = util.TrueLinePosition;
bool flag4 = reader.IsEmptyElement;
if (declarationUpdates == null)
{
util.CopyOuterXmlToNextElement(utilWriter, true);
}
else
{
string str3 = util.UpdateStartElement(utilWriter, null, true, linePosition, indent);
if (!flag4)
{
util.CopyReaderToNextElement(utilWriter, true);
this.CopyConfigDeclarationsRecursive(declarationUpdates, util, utilWriter, string.Empty, linePosition, indent);
}
if (declarationUpdates.HasUnretrievedSections())
{
int num4 = 0;
if (str3 == null)
{
num4 = util.TrueLinePosition;
}
if (!utilWriter.IsLastLineBlank)
{
utilWriter.AppendNewLine();
}
this.WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, linePosition + indent, indent, false);
if (str3 == null)
{
utilWriter.AppendSpacesToLinePosition(num4);
}
}
if (str3 == null)
{
util.CopyXmlNode(utilWriter);
}
else
{
utilWriter.Write(str3);
}
util.CopyReaderToNextElement(utilWriter, true);
}
}
}
if (!flag3 && (declarationUpdates != null))
{
int num5;
bool skipFirstIndent = (reader.Depth > 0) && (reader.NodeType == XmlNodeType.Element);
if (skipFirstIndent)
{
num5 = util.TrueLinePosition;
}
else
{
num5 = trueLinePosition + indent;
}
this.WriteNewConfigDeclarations(declarationUpdates, utilWriter, num5, indent, skipFirstIndent);
}
if (definitionUpdates != null)
{
bool locationPathApplies = false;
LocationUpdates locationUpdates = null;
SectionUpdates sectionUpdates = null;
if (!base.IsLocationConfig)
//.........这里部分代码省略.........
示例10: CopyConfigDeclarationsRecursive
private bool CopyConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, string group, int parentLinePosition, int parentIndent)
{
int trueLinePosition;
int num3;
bool flag = false;
XmlTextReader reader = xmlUtil.Reader;
int oldIndent = this.UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);
if (reader.NodeType == XmlNodeType.Element)
{
trueLinePosition = xmlUtil.TrueLinePosition;
num3 = trueLinePosition;
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
trueLinePosition = parentLinePosition + oldIndent;
if (utilWriter.IsLastLineBlank)
{
num3 = xmlUtil.TrueLinePosition;
}
else
{
num3 = parentLinePosition;
}
}
else
{
trueLinePosition = parentLinePosition + oldIndent;
num3 = 0;
}
if (declarationUpdates != null)
{
string[] movedSectionNames = declarationUpdates.GetMovedSectionNames();
if (movedSectionNames != null)
{
if (!utilWriter.IsLastLineBlank)
{
utilWriter.AppendNewLine();
}
foreach (string str in movedSectionNames)
{
DeclarationUpdate declarationUpdate = declarationUpdates.GetDeclarationUpdate(str);
utilWriter.AppendSpacesToLinePosition(trueLinePosition);
utilWriter.Write(declarationUpdate.UpdatedXml);
utilWriter.AppendNewLine();
flag = true;
}
utilWriter.AppendSpacesToLinePosition(num3);
}
}
if (reader.NodeType == XmlNodeType.Element)
{
int depth = reader.Depth;
while (reader.Depth == depth)
{
bool flag2 = false;
DeclarationUpdate update2 = null;
DeclarationUpdate sectionGroupUpdate = null;
SectionUpdates sectionUpdatesForGroup = null;
SectionUpdates updates2 = declarationUpdates;
string str2 = group;
oldIndent = this.UpdateIndent(oldIndent, xmlUtil, utilWriter, parentLinePosition);
trueLinePosition = xmlUtil.TrueLinePosition;
string name = reader.Name;
string attribute = reader.GetAttribute("name");
string configKey = BaseConfigurationRecord.CombineConfigKey(group, attribute);
if (name == "sectionGroup")
{
sectionUpdatesForGroup = declarationUpdates.GetSectionUpdatesForGroup(attribute);
if (sectionUpdatesForGroup != null)
{
sectionGroupUpdate = sectionUpdatesForGroup.GetSectionGroupUpdate();
if (sectionUpdatesForGroup.HasUnretrievedSections())
{
flag2 = true;
str2 = configKey;
updates2 = sectionUpdatesForGroup;
}
}
}
else
{
update2 = declarationUpdates.GetDeclarationUpdate(configKey);
}
bool flag3 = (sectionGroupUpdate != null) && (sectionGroupUpdate.UpdatedXml != null);
if (flag2)
{
object o = utilWriter.CreateStreamCheckpoint();
string s = null;
if (flag3)
{
utilWriter.Write(sectionGroupUpdate.UpdatedXml);
reader.Read();
}
else
{
s = xmlUtil.UpdateStartElement(utilWriter, null, true, trueLinePosition, oldIndent);
}
if (s == null)
{
xmlUtil.CopyReaderToNextElement(utilWriter, true);
//.........这里部分代码省略.........
示例11: CheckPreamble
private void CheckPreamble(byte[] preamble, XmlUtilWriter utilWriter, byte[] buffer)
{
bool flag = false;
using (Stream stream = new MemoryStream(buffer))
{
byte[] buffer2 = new byte[preamble.Length];
if (stream.Read(buffer2, 0, buffer2.Length) == buffer2.Length)
{
flag = true;
for (int i = 0; i < buffer2.Length; i++)
{
if (buffer2[i] != preamble[i])
{
flag = false;
goto Label_004A;
}
}
}
}
Label_004A:
if (!flag)
{
object o = utilWriter.CreateStreamCheckpoint();
utilWriter.Write('x');
utilWriter.RestoreStreamCheckpoint(o);
}
}
示例12: WriteUnwrittenConfigDeclarationsRecursive
private void WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent)
{
string[] unretrievedSectionNames = declarationUpdates.GetUnretrievedSectionNames();
if (unretrievedSectionNames != null)
{
foreach (string str in unretrievedSectionNames)
{
if (!skipFirstIndent)
{
utilWriter.AppendSpacesToLinePosition(linePosition);
}
skipFirstIndent = false;
DeclarationUpdate declarationUpdate = declarationUpdates.GetDeclarationUpdate(str);
if ((declarationUpdate != null) && !string.IsNullOrEmpty(declarationUpdate.UpdatedXml))
{
utilWriter.Write(declarationUpdate.UpdatedXml);
utilWriter.AppendNewLine();
}
}
}
string[] unretrievedGroupNames = declarationUpdates.GetUnretrievedGroupNames();
if (unretrievedGroupNames != null)
{
foreach (string str2 in unretrievedGroupNames)
{
if (base.TargetFramework != null)
{
ConfigurationSectionGroup sectionGroup = this.GetSectionGroup(str2);
if ((sectionGroup != null) && !sectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework))
{
declarationUpdates.MarkGroupAsRetrieved(str2);
continue;
}
}
if (!skipFirstIndent)
{
utilWriter.AppendSpacesToLinePosition(linePosition);
}
skipFirstIndent = false;
SectionUpdates sectionUpdatesForGroup = declarationUpdates.GetSectionUpdatesForGroup(str2);
DeclarationUpdate sectionGroupUpdate = sectionUpdatesForGroup.GetSectionGroupUpdate();
if (sectionGroupUpdate == null)
{
utilWriter.Write("<sectionGroup name=\"" + str2 + "\">");
}
else
{
utilWriter.Write(sectionGroupUpdate.UpdatedXml);
}
utilWriter.AppendNewLine();
this.WriteUnwrittenConfigDeclarationsRecursive(sectionUpdatesForGroup, utilWriter, linePosition + indent, indent, false);
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write("</sectionGroup>\r\n");
}
}
}
示例13: WriteSectionUpdate
private void WriteSectionUpdate(XmlUtilWriter utilWriter, DefinitionUpdate update, int linePosition, int indent, bool skipFirstIndent)
{
string updatedXml;
ConfigurationSection result = (ConfigurationSection) update.SectionRecord.Result;
if (!string.IsNullOrEmpty(result.SectionInformation.ConfigSource))
{
updatedXml = string.Format(CultureInfo.InvariantCulture, "<{0} configSource=\"{1}\" />", new object[] { result.SectionInformation.Name, result.SectionInformation.ConfigSource });
}
else
{
updatedXml = update.UpdatedXml;
}
string s = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, skipFirstIndent);
utilWriter.Write(s);
}
示例14: 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;
//.........这里部分代码省略.........
示例15: CreateNewConfig
private void CreateNewConfig(SectionUpdates declarationUpdates, ConfigDefinitionUpdates definitionUpdates, NamespaceChange namespaceChange, XmlUtilWriter utilWriter)
{
int linePosition = 5;
int indent = 4;
utilWriter.Write(string.Format(CultureInfo.InvariantCulture, "<?xml version=\"1.0\" encoding=\"{0}\"?>\r\n", new object[] { base.ConfigStreamInfo.StreamEncoding.WebName }));
if (namespaceChange == NamespaceChange.Add)
{
utilWriter.Write(string.Format(CultureInfo.InvariantCulture, "<configuration xmlns=\"{0}\">\r\n", new object[] { "http://schemas.microsoft.com/.NetConfiguration/v2.0" }));
}
else
{
utilWriter.Write("<configuration>\r\n");
}
if (declarationUpdates != null)
{
this.WriteNewConfigDeclarations(declarationUpdates, utilWriter, linePosition, indent, false);
}
this.WriteNewConfigDefinitions(definitionUpdates, utilWriter, linePosition, indent);
utilWriter.Write("</configuration>");
}