我正在尝试重构一个将其对象作为 XML 传输的库。虽然我认为.NET Framework 的XmlSerialzer 可以处理序列化,但该类都有一个ToXML
函数。在其中,所有字符串值都通过一个转义字符的函数
喜欢和相似。
XmlSerializer 不会自动转义那些 a 字符吗?
最佳答案
是的。
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
namespace TestXmlSerialiser
{
public class Person
{
public string Name;
}
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "Jack & Jill";
XmlSerializer ser = new XmlSerializer(typeof(Person));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{
ser.Serialize(writer, person);
}
}
}
}
返回
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Jack & Jill</Name>
</Person>
关于c# - XmlSerializer 是否会转义 & 等特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1266198/