Does XmlSerializer call special characters like &?

I am trying to reorganize a library that passes an object as XML. Although I think the XmlSerialzer.NET Framework can handle serialization, the class has a function ToXML. In it, all string values ​​are passed through a function that escapes characters like so.

Can the XmlSerializer automatically avoid these character types?

+5
source share
2 answers

Yes Yes.

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 &amp; Jill</Name>
</Person>
+10
source

All XML-API.NET naturally understand XML rules. If necessary, they will change <to &lt;, etc.

+1
source

All Articles