XmlSpace xmlSpace = XmlSpace.None;
bool openXmlLang = false;
bool openXmlSpace = false;
+ string openElementPrefix;
+ string openElementNS;
+ bool hasRoot = false;
#endregion
#endregion
#region Methods
+ private void AddMissingElementXmlns ()
+ {
+ // output namespace declaration if not exist.
+ string prefix = openElementPrefix;
+ string ns = openElementNS;
+ if (ns != null/* && LookupPrefix (ns) != prefix*/)
+ {
+ string formatXmlns = String.Empty;
+ if (ns != String.Empty)
+ {
+ string existingPrefix = namespaceManager.LookupPrefix (ns);
+ bool addDefaultNamespace = false;
+
+ if (existingPrefix == null)
+ {
+ namespaceManager.AddNamespace (prefix, ns);
+ addDefaultNamespace = true;
+ }
+
+ if (prefix == String.Empty)
+ prefix = existingPrefix;
+
+ if (prefix != existingPrefix)
+ formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", prefix, quoteChar, ns);
+ else if (addDefaultNamespace)
+ formatXmlns = String.Format (" xmlns={0}{1}{0}", quoteChar, ns);
+ }
+ else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace (prefix) != String.Empty))
+ {
+ namespaceManager.AddNamespace (prefix, ns);
+ formatXmlns = String.Format (" xmlns={0}{0}", quoteChar);
+ }
+ if(formatXmlns != String.Empty)
+ w.Write(formatXmlns);
+ openElementPrefix = null;
+ openElementNS = null;
+ }
+ }
private void CheckState ()
{
throw new InvalidOperationException ("The Writer is closed.");
}
if ((documentStarted == true) && (formatting == Formatting.Indented) && (!IndentingOverriden)) {
- indentFormatting = "\r\n";
+ indentFormatting = w.NewLine;
if (indentLevel > 0) {
for (int i = 0; i < indentLevel; i++)
indentFormatting += indentChars;
private void CloseStartElement ()
{
- if (openStartElement) {
- w.Write(">");
- ws = WriteState.Content;
- openStartElement = false;
- attributeWrittenForElement = false;
- }
+ if (!openStartElement)
+ return;
+
+ AddMissingElementXmlns ();
+
+ w.Write (">");
+ ws = WriteState.Content;
+ openStartElement = false;
+ attributeWrittenForElement = false;
}
public override void Flush ()
{
string prefix = namespaceManager.LookupPrefix (ns);
- if (prefix == String.Empty)
- prefix = null;
-
+ // XmlNamespaceManager has changed to return null when NSURI not found.
+ // (Contradiction to the documentation.)
+ //if (prefix == String.Empty)
+ // prefix = null;
return prefix;
}
{
CloseOpenAttributeAndElements ();
- if ((ws == WriteState.Start) || (ws == WriteState.Prolog))
+ if (!hasRoot)
throw new ArgumentException ("This document does not have a root element.");
ws = WriteState.Start;
+ hasRoot = false;
}
public override void WriteEndElement ()
indentLevel--;
CheckState ();
+ AddMissingElementXmlns ();
if (openStartElement) {
if (openAttribute)
WriteStringInternal (data, false);
}
- [MonoTODO]
public override void WriteRaw (char[] buffer, int index, int count)
{
- throw new NotImplementedException ();
+ WriteStringInternal (new string (buffer, index, count), false);
}
public override void WriteStartAttribute (string prefix, string localName, string ns)
string existingPrefix = namespaceManager.LookupPrefix (ns);
if (prefix == String.Empty)
- prefix = existingPrefix;
+ prefix = (existingPrefix == null) ?
+ String.Empty : existingPrefix;
}
if (prefix != String.Empty)
if (documentStarted == true)
throw new InvalidOperationException("WriteStartDocument should be the first call.");
+ if (hasRoot)
+ throw new XmlException ("WriteStartDocument called twice.");
+
+ hasRoot = true;
+
CheckState ();
string encodingFormatting = "";
private void WriteStartElementInternal (string prefix, string localName, string ns)
{
- if (prefix == null)
- prefix = String.Empty;
-
- if ((prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
+ if ((prefix != null && prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
CheckState ();
CloseStartElement ();
+ if (prefix == null)
+ prefix = namespaceManager.LookupPrefix (ns);
+ if (prefix == null)
+ prefix = String.Empty;
+
string formatXmlns = "";
string formatPrefix = "";
- if(ns != null)
- {
- if (ns != String.Empty)
- {
- string existingPrefix = namespaceManager.LookupPrefix (ns);
- bool addDefaultNamespace = false;
-
- if (existingPrefix == String.Empty && !namespaceManager.HasNamespace (prefix)) {
- namespaceManager.AddNamespace (prefix, ns);
- addDefaultNamespace = true;
- }
- if (prefix == String.Empty)
- prefix = existingPrefix;
-
- if (prefix != existingPrefix)
- formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", prefix, quoteChar, ns);
- else if (addDefaultNamespace)
- formatXmlns = String.Format (" xmlns={0}{1}{0}", quoteChar, ns);
- }
- else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace(prefix) != String.Empty)) {
- formatXmlns = String.Format (" xmlns={0}{0}", quoteChar);
- }
-
- if (prefix != String.Empty) {
+ if(ns != null) {
+ if (prefix != String.Empty)
formatPrefix = prefix + ":";
- }
}
w.Write ("{0}<{1}{2}{3}", indentFormatting, formatPrefix, localName, formatXmlns);
openElements.Push (new XmlTextWriterOpenElement (formatPrefix + localName));
ws = WriteState.Element;
openStartElement = true;
+ openElementNS = ns;
+ openElementPrefix = prefix;
namespaceManager.PushScope ();
- if(ns != null)
- {
- namespaceManager.AddNamespace (prefix, ns);
- }
+// if(ns != null)
+// namespaceManager.AddNamespace (prefix, ns);
indentLevel++;
}