[英]Does the XmlSerializer escape special chars like &?
I'm trying to refactor a library that's transmits it's object as XML. 我正在尝试重构一个将它的对象作为XML传输的库。 Although I think the XmlSerialzer of the .NET Framework can handle the serialization, the class all have a ToXML
function. 虽然我认为.NET Framework的XmlSerialzer可以处理序列化,但该类都具有ToXML
功能。 In it all string values are being put through a function that escapes characters like & and alike. 在其中,所有字符串值都通过一个函数来进行,该函数可以转义像&等相似的字符。
Does the XmlSerializer not escape those kind a characters automatically? XmlSerializer是否不会自动转义那些字符?
Yes it does. 是的,它确实。
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);
}
}
}
}
returns 回报
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Jack & Jill</Name>
</Person>
All of the .NET XML APIs naturally understand the rules of XML. 所有.NET XML API都自然地理解XML的规则。 If necessary, they will change <
into <
如有必要,他们会改变<
into <
etc. 等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.