5 // Copyright 2007 Novell (http://www.novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
31 namespace System.Xml.Linq
33 public class XStreamingElement
35 public XStreamingElement (XName name)
40 public XStreamingElement (XName name, object content)
46 public XStreamingElement (XName name, params object [] content)
53 List<object> contents;
60 internal IEnumerable<object> Contents {
61 get { return contents; }
64 public void Add (object content)
67 contents = new List<object> ();
68 contents.Add (content);
71 public void Add (params object [] content)
74 contents = new List<object> ();
75 contents.Add (content);
78 public void Save (string fileName)
80 using (TextWriter w = File.CreateText (fileName))
81 Save (w, SaveOptions.None);
84 public void Save (TextWriter textWriter)
86 Save (textWriter, SaveOptions.None);
89 public void Save (XmlWriter writer)
94 public void Save (string fileName, SaveOptions options)
96 using (TextWriter w = File.CreateText (fileName))
100 public void Save (TextWriter textWriter, SaveOptions options)
102 XmlWriterSettings s = new XmlWriterSettings ();
103 s.OmitXmlDeclaration = true;
105 if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
108 if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
109 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
111 using (XmlWriter w = XmlWriter.Create (textWriter, s))
116 public void Save (Stream stream)
118 Save (stream, SaveOptions.None);
121 public void Save (Stream stream, SaveOptions options)
123 XmlWriterSettings s = new XmlWriterSettings ();
124 if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
126 if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
127 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
129 using (var writer = XmlWriter.Create (stream, s)){
135 public override string ToString ()
137 return ToString (SaveOptions.None);
140 public string ToString (SaveOptions options)
142 StringWriter sw = new StringWriter ();
144 return sw.ToString ();
147 public void WriteTo (XmlWriter writer)
149 writer.WriteStartElement (name.LocalName, name.Namespace.NamespaceName);
150 WriteContents (contents, writer);
151 writer.WriteEndElement ();
154 void WriteContents (IEnumerable<object> items, XmlWriter w)
156 foreach (object o in XUtil.ExpandArray (items)) {
159 else if (o is XStreamingElement)
160 ((XStreamingElement) o).WriteTo (w);
162 ((XNode) o).WriteTo (w);
163 else if (o is object [])
164 WriteContents ((object []) o, w);
165 else if (o is XAttribute)
166 WriteAttribute ((XAttribute) o, w);
168 new XText (o.ToString ()).WriteTo (w);
172 void WriteAttribute (XAttribute a, XmlWriter w)
174 if (a.IsNamespaceDeclaration) {
175 if (a.Name.Namespace == XNamespace.Xmlns)
176 w.WriteAttributeString ("xmlns", a.Name.LocalName, XNamespace.Xmlns.NamespaceName, a.Value);
178 w.WriteAttributeString ("xmlns", a.Value);
181 w.WriteAttributeString (a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value);