using System.Globalization;
using System.IO;
using System.Linq;
+using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
[Test]
[ExpectedException (typeof (ArgumentException))]
[Category ("NotDotNet")]
+ [Ignore ("see inline comment")]
public void AddXDeclarationToElement ()
{
XElement el = new XElement (XName.Get ("foo"));
[Test]
[ExpectedException (typeof (ArgumentException))]
[Category ("NotDotNet")]
- [Category ("NotWorking")]
+ [Ignore ("see code comment")]
// LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
public void AddAfterSelfXDeclaration ()
{
{
XElement a = new XElement ("a", "7");
XElement b = new XElement ("b", new XCData (" 42 "));
- XElement c = new XElement ("c", " \r\n 13 \t ");
+ XElement c = new XElement ("c", " \r\n 13 \t ".Replace ("\r\n", Environment.NewLine));
XElement d = new XElement ("d", -101);
XElement o = new XElement ("o", "0");
XElement l = new XElement ("l", "1");
// Verify expected "cloning" and basic conversion behaviour as prerequisites
Assert.IsFalse (a.IsEmpty, "#1-1");
Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
- Assert.AreEqual (" \r\n 13 \t ", c.Value, "#2-1");
+ Assert.AreEqual (" \r\n 13 \t ".Replace ("\r\n", Environment.NewLine), c.Value, "#2-1");
Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
Assert.AreNotSame (o, new XElement (o), "#3-1");
Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
Assert.AreEqual ("<b><![CDATA[ 42 ]]></b>", b.ToString (), "#3-3b");
- Assert.AreEqual ("<c> \r\n 13 \t </c>", c.ToString (), "#3-3c");
+ Assert.AreEqual ("<c> \r\n 13 \t </c>".Replace ("\r\n", Environment.NewLine), c.ToString (), "#3-3c");
Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
Assert.AreEqual ("7", (string) new XElement (a), "a:string");
Assert.AreEqual (" 42 ", (string) new XElement (b), "b:string");
- Assert.AreEqual (" \r\n 13 \t ", (string) new XElement (c), "c:string");
+ Assert.AreEqual (" \r\n 13 \t ".Replace ("\r\n", Environment.NewLine), (string) new XElement (c), "c:string");
Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
Assert.AreEqual ("0", (string) new XElement (o), "o:string");
Assert.AreEqual ("1", (string) new XElement (l), "l:string");
// LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
[ExpectedException (typeof (ArgumentException))]
[Category ("NotDotNet")]
+ [Ignore ("see code comment")]
public void SetValueXDeclaration ()
{
var el = new XElement ("foo");
public XElement Content;
}
-#if NET_4_5
[Test]
// Bug #12571
public void DeserializeXElement ()
var xe = (SerializableClass)res;
Assert.AreEqual (xe.Content.ToString (), "<Data />", "#3");
}
-#endif
+
+ [XmlType ("Root")]
+ public class DeserializeXElementArray_Data
+ {
+ [XmlAnyElement]
+ public XElement[] Content;
+ }
+
+ [Test]
+ public void DeserializeXElementArray ()
+ {
+ var xmlString = "<Root><Data /></Root>";
+
+ var serializer = new XmlSerializer (typeof (DeserializeXElementArray_Data));
+ var res = serializer.Deserialize (new StringReader (xmlString));
+
+ Assert.IsNotNull (res, "#1");
+ Assert.AreEqual (typeof (DeserializeXElementArray_Data), res.GetType (), "#2");
+ var xe = (DeserializeXElementArray_Data)res;
+ Assert.AreEqual (xe.Content [0].ToString (), "<Data />", "#3");
+ }
+
+ [Test] // Bug #20151
+ public void XElementFromArrayWithNullValuesAsObject ()
+ {
+ string[] content = {null, "content1", null, "content2"};
+ var el = new XElement ("test", (object)content);
+ Assert.AreEqual ("<test>content1content2</test>", el.ToString ());
+ }
+
+ [Test] // bug #14856
+ public void PossibleDuplicateNamespaces ()
+ {
+ string testXML =
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
+ "<TestElement xmlns=\"http://www.test.com/TestNamespace\" />";
+
+ using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (testXML)))
+ {
+ var root = XElement.Load (stream);
+ using (var savedStream = new MemoryStream ())
+ {
+ var options = SaveOptions.None;
+
+ // Comment out this line to make it not crash.
+ options |= SaveOptions.OmitDuplicateNamespaces;
+
+ root.Save (savedStream, options);
+ savedStream.Flush ();
+ savedStream.Position = 0;
+
+ var settings = new XmlReaderSettings { IgnoreComments = true, IgnoreWhitespace = true };
+ using (var xmlReader = XmlReader.Create (savedStream, settings))
+ {
+ while (xmlReader.Read ())
+ {
+ }
+ }
+ }
+ }
+ }
+
+ [Test]
+ public void ParseVsReadXml ()
+ {
+ var p = XElement.Parse ("<root xmlns='urn:foo'><foo><xxx /></foo><x:bar xmlns:x='urn:bar'><yyy /></x:bar><baz xmlns=''><zzz /></baz></root>");
+ var r = XElement.Parse ("<foo />");
+ XmlReader xr = XmlReader.Create (new StringReader ("<root xmlns='urn:foo'><foo><xxx /></foo><x:bar xmlns:x='urn:bar'><yyy /></x:bar><baz xmlns=''><zzz /></baz></root>"), new XmlReaderSettings ());
+ ((IXmlSerializable)r).ReadXml (xr);
+
+ Assert.IsTrue (XNode.DeepEquals (p, r), "The XElements were not equal.\nParse() expected:\n{0}\n\nBut ReadXml() was:\n{1}\n", p, r);
+ }
}
}