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.
31 using System.Xml.Linq;
32 using System.Xml.Serialization;
34 using NUnit.Framework;
36 using XPI = System.Xml.Linq.XProcessingInstruction;
38 namespace MonoTests.System.Xml.Linq
41 public class XNodeWriterTest
44 public void WriteEmptyElements ()
46 var doc = new XDocument ();
47 XmlWriter w = doc.CreateWriter ();
48 w.WriteStartElement ("root");
49 w.WriteStartElement ("foo");
51 w.WriteStartElement ("bar");
52 w.WriteFullEndElement ();
55 Assert.IsTrue (((XElement) doc.Root.FirstNode).IsEmpty, "#1");
56 Assert.IsFalse (((XElement) doc.Root.LastNode).IsEmpty, "#2");
60 public void CreateWriter1 ()
62 string xml = "<root><foo/><bar></bar><baz a='v' xmlns='urn:foo' xmlns:x='urn:x'><x:ext xmlns=''>test</x:ext><!-- comment --> <?some-pi some-data?></baz></root>";
63 XDocument doc = new XDocument ();
64 XmlWriter xw = doc.CreateWriter ();
65 XmlReader xr = XmlReader.Create (new StringReader (xml));
67 xw.WriteNode (xr, false);
70 Assert.AreEqual ("root", doc.Root.Name.LocalName, "#1");
71 XElement el = doc.Root.FirstNode as XElement;
72 Assert.AreEqual ("foo", el.Name.LocalName, "#2-1");
73 Assert.IsTrue (el.IsEmpty, "#2-2");
74 Assert.IsFalse (el.HasAttributes, "#2-3");
75 el = el.NextNode as XElement;
76 Assert.IsFalse (el.IsEmpty, "#3");
77 el = el.NextNode as XElement;
78 Assert.AreEqual ("a", el.FirstAttribute.Name.LocalName, "#4-1");
79 Assert.AreEqual ("xmlns", el.FirstAttribute.NextAttribute.Name.LocalName, "#4-2");
80 Assert.AreEqual ("x", el.LastAttribute.Name.LocalName, "#4-3");
81 Assert.AreEqual (XNamespace.Xmlns, el.LastAttribute.Name.Namespace, "#4-4");
82 el = el.FirstNode as XElement;
84 Assert.AreEqual ("ext", el.Name.LocalName, "#5-1");
85 Assert.AreEqual (XNamespace.Get ("urn:x"), el.Name.Namespace, "#5-2");
87 Assert.AreEqual ("xmlns", el.FirstAttribute.Name.LocalName, "#5-3");
88 Assert.AreEqual (XNamespace.Get (String.Empty), el.FirstAttribute.Name.Namespace, "#5-4");
89 XText t = el.FirstNode as XText;
90 Assert.AreEqual ("test", t.Value, "#6");
91 XComment c = el.NextNode as XComment;
92 Assert.AreEqual (" comment ", c.Value, "#7");
93 t = c.NextNode as XText;
94 Assert.AreEqual (" ", t.Value, "#8");
95 XPI pi = t.NextNode as XPI;
96 Assert.AreEqual ("some-pi", pi.Target, "#9-1");
97 Assert.AreEqual ("some-data", pi.Data, "#9-2");
98 Assert.IsNull (el.Parent.NextNode, "#10");
99 Assert.IsNull (el.Parent.Parent.NextNode, "#11");
102 [XmlRoot ("MyRoot", Namespace = "urn:mynamespace")]
106 public MyElementBase[] Elements;
109 [XmlInclude (typeof(MyElement1))]
110 public abstract class MyElementBase
114 [XmlType ("MyElement1", Namespace = "urn:mynamespace")]
115 public class MyElement1 : MyElementBase
122 public void Bug24300 ()
124 XmlSerializer serializer = new XmlSerializer (typeof (MyData));
125 MyData data = new MyData () { Elements = new MyElementBase [] { new MyElement1 { V = 2 } } };
127 XDocument doc = new XDocument ();
128 using (var writer = doc.CreateWriter ())
130 serializer.Serialize (writer, data);
133 var sb = new StringBuilder ();
134 sb.Append ("<MyRoot xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn:mynamespace\">\r\n");
135 sb.Append (" <Elements xsi:type=\"MyElement1\" V=\"2\" />\r\n");
136 sb.Append ("</MyRoot>");
138 Assert.AreEqual (sb.ToString (), doc.Root.ToString ());