//
// 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 = 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);
}