Skip to content

Instantly share code, notes, and snippets.

@Andrea
Created September 21, 2010 08:57
Show Gist options
  • Save Andrea/589424 to your computer and use it in GitHub Desktop.
Save Andrea/589424 to your computer and use it in GitHub Desktop.
[Fact] //this is a the test attribute when using xUnit
public void When_xml_Something_Then_serialize_returns_null()
{
string serializedObject =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><MyClass xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://exampleA.org\" />";
using (var stringReader = new StringReader(serializedObject))
{
Assert.Null(new XmlSerializer(typeof(MyClass)).Deserialize(stringReader));
}
}
[Fact]
public void When_xml_Then_serialize_returns_null_with_xml_reader()
{
string serializedObject =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><MyClass xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://exampleA.org\" />";
using (var stringReader = new StringReader(serializedObject))
{
var doc = new XmlDocument();
doc.Load(stringReader);
Assert.Null(new XmlSerializer(typeof(MyClass)).Deserialize(stringReader));
}
}
//---------------------------
[Serializable]
[XmlRoot(Namespace = "http://example.org")]
public class MyClass
{
}
@Andrea
Copy link
Author

Andrea commented Sep 21, 2010

the test throws

System.InvalidOperationException: was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderMyClass.Read3_MyClass()
System.InvalidOperationException: There is an error in XML document (1, 40).

if you change the xmlns:xsd or the xmlns:xsi then the class is deserialized no probs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment