在 Mono 中编写 NUnit 测试并在 Java 中编写 JUnit 测试后,答案似乎是使用 <tag>Hello
\nWorld!</tag> 或 <tag>Hello
\nWorld!</tag>如下...
Foo.cs:
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace XmlStringTests
{
public class Foo
{
public string greeting;
public static Foo DeserializeFromXmlString (string xml)
{
Foo result;
using (MemoryStream memoryStream = new MemoryStream()) {
byte[] buffer = Encoding.UTF8.GetBytes (xml);
memoryStream.Write (buffer, 0, buffer.Length);
memoryStream.Seek (0, SeekOrigin.Begin);
XmlSerializer xs = new XmlSerializer (typeof(Foo));
result = (Foo)xs.Deserialize (memoryStream);
}
return result;
}
}
}
XmlStringTests.cs:
using NUnit.Framework;
namespace XmlStringTests
{
[TestFixture]
public class XmlStringTests
{
const string expected = "Hello\u000d\u000aWorld!";
[Test(Description="Fails")]
public void Cdata ()
{
const string test = "<Foo><greeting><![CDATA[Hello\u000d\u000aWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Fails")]
public void CdataWithHash13 ()
{
const string test = "<Foo><greeting><![CDATA[Hello \u000aWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Fails")]
public void CdataWithHashxD ()
{
const string test = "<Foo><greeting><![CDATA[Hello
\u000aWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Fails")]
public void Simple ()
{
const string test = "<Foo><greeting>Hello\u000d\u000aWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Passes")]
public void SimpleWithHash13 ()
{
const string test = "<Foo><greeting>Hello \u000aWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Passes")]
public void SimpleWithHashxD ()
{
const string test = "<Foo><greeting>Hello
\u000aWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
}
}
Foo.java:
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Foo")
@XmlType(propOrder = { "greeting" })
public class Foo {
public String greeting;
public static Foo DeserializeFromXmlString(String xml) {
try {
JAXBContext context = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Foo foo = (Foo) unmarshaller.unmarshal(new StringReader(xml));
return foo;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
}
XmlStringTests.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class XmlStringTests {
String expected = "Hello\r\nWorld!";
@Test //Fails
public void testCdata ()
{
String test = "<Foo><greeting><![CDATA[Hello\r\nWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Fails
public void testCdataWithHash13 ()
{
String test = "<Foo><greeting><![CDATA[Hello \nWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Fails
public void testCdataWithHashxD ()
{
String test = "<Foo><greeting><![CDATA[Hello
\nWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Fails
public void testSimple ()
{
String test = "<Foo><greeting>Hello\r\nWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Passes
public void testSimpleWithHash13 ()
{
String test = "<Foo><greeting>Hello \nWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Passes
public void testSimpleWithHashxD ()
{
String test = "<Foo><greeting>Hello
\nWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
}
我希望这可以节省一些人一些时间。