C# 序列化会在使用&;时更改属性值;书信电报;或;燃气轮机;(&&x27;<;&x27;或&&x27;>;&x27;)

C# 序列化会在使用&;时更改属性值;书信电报;或;燃气轮机;(&&x27;<;&x27;或&&x27;>;&x27;),c#,xml,serialization,xsd,deserialization,C#,Xml,Serialization,Xsd,Deserialization,给出以下一段xml代码: <SD GID="&lt;empty&gt;" T="2017-07-31T13:37:48Z">&lt;empty&gt;</SD> 属性中这些字符的错误翻译可能是什么原因?NMTOKEN不能有效地包含(即使这些字符被转义为字符引用),因此序列化程序找到了自己的方法将属性值转换为有效的NMTOKEN。否,它没有标记。如果要删除DataType=“NMTOKEN”,问题将得到解决。在XML文件(Java和DOM解

给出以下一段xml代码:

<SD GID="&lt;empty&gt;" T="2017-07-31T13:37:48Z">&lt;empty&gt;</SD>

属性中这些字符的错误翻译可能是什么原因?

NMTOKEN不能有效地包含
(即使这些字符被转义为字符引用),因此序列化程序找到了自己的方法将属性值转换为有效的NMTOKEN。

否,它没有标记。如果要删除
DataType=“NMTOKEN”
,问题将得到解决。在XML文件(Java和DOM解析器)中,“”作为节点文本值存在,但当node.getContentType用于此节点时,它将转换为“对于我们这些没有在准则中指定NMTOKEN的人,我们该怎么办?你问一个新问题,从第一原则描述你的问题,而不是对两年前提出的不同问题的答案发表评论。
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://autosar.org/schema/r4.0")]
public partial class SD {
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "NMTOKEN")]
    public string GID;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string S;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string T;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value;
}
<SD GID="_x003C_empty_x003E_" T="2017-07-31T13:37:48Z">&lt;empty&gt;</SD>
namespace MyXMLHandler
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    internal class Program
    {
        private static void Main(string[] args)
        {
            MyType a = DeserializeObject(
                @"source.arxml");
            SerializeObject(
                @"source_SERIALIZED.arxml",
                a);
        }

        private static MyType DeserializeObject(string filename)
        {
            var serializer = new XmlSerializer(typeof(MyType));
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            StreamReader reader = new StreamReader(filename, Encoding.UTF8);
            MyType i;

            i = (MyType)serializer.Deserialize(reader);

            return i;
        }

        private static void SerializeObject(string filename, MyType i)
        {
            var serializer = new XmlSerializer(typeof(MyType));
            Stream fs = new FileStream(filename, FileMode.Create);
            var settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "    ";
            settings.Encoding = Encoding.UTF8;
            var writer = XmlWriter.Create(fs, settings);

            serializer.Serialize(writer, i);

            writer.Close();

        }
    }
}