与可以执行任何操作的application / xml文件或者将所有空白序列转换为单个空格字符的normalizedString值相反,我在这里特别要求在具有字符串值的text / xml文件的上下文中。为简单起见,假设我只使用带有UTF8编码文件的ASCII字符。
给出以下两行文本字符串,我希望用XML表示:
Hello
World!
内存中的以下字节:
0000: 48 65 6c 6c 6f 0d 0a 57 6f 72 6c 64 21 Hello..World!
根据RFC 2046,任何text / * MIME类型必须(不应该)使用Carriage Return后跟Linefeed字符序列表示换行符。有鉴于此,以下XML片段应该是正确的:
<tag>Hello
World!</tag>
或
0000: 3c 74 61 67 3c 48 65 6c 6c 6f 0d 0a 57 6f 72 6c <tag>Hello..Worl
0010: 64 21 3c 2f 74 61 67 3c d!</tag>
但我经常看到如下文件:
<tag><![CDATA[Hello
World!]]></tag>
或者,甚至是陌生人:
<tag>Hello&xD;
World!</tag>
&amp; 0xD;序列之后是一个换行符:
0000: 3c 74 61 67 3c 48 65 6c 6c 6f 26 78 44 3b 0a 57 <tag>Hello&xD;.W
0010: 6f 72 6c 64 21 3c 2f 74 61 67 3c orld!</tag>
我在这里缺少什么?在XML字符串值中表示多行文本的正确方法是什么,以便它可以在另一端不受干扰地出现?
答案 0 :(得分:2)
CR(&amp; x0D;),LF(&amp; x0A;),CRLF或其他一些组合都是有效的。正如the spec中所述,所有这些都被翻译成单个&amp; x0A;字符。
答案 1 :(得分:2)
在用Java编写Mono和JUnit测试中的NUnit测试后,答案似乎是使用&lt; tag&gt; Hello&amp;#13; \ nWorld!&lt; / tag&gt;或&lt; tag&gt; Hello&amp; #xd; \ nWorld!&lt; / tag&gt;如下......
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);
}
}
我希望这能节省一些人的时间。
答案 2 :(得分:0)
我发现的唯一适用于
使用 GUID 而不是固定的转义序列意味着不会意外与已经存在的数据发生冲突。