是否可以通过某种类型的属性,使用.Net XmlSerializer?

推荐答案

[XmlRoot("root")]
public class Sample1Xml
{
    internal Sample1Xml()
    {
    }

    [XmlElement("node")]
    public NodeType Node { get; set; }

    #region Nested type: NodeType

    public class NodeType
    {
        [XmlAttribute("attr1")]
        public string Attr1 { get; set; }

        [XmlAttribute("attr2")]
        public string Attr2 { get; set; }

        [XmlIgnore]
        public string Content { get; set; }

        [XmlText]
        public XmlNode[] CDataContent
        {
            get
            {
                var dummy = new XmlDocument();
                return new XmlNode[] {dummy.CreateCDataSection(Content)};
            }
            set
            {
                if (value == null)
                {
                    Content = null;
                    return;
                }

                if (value.Length != 1)
                {
                    throw new InvalidOperationException(
                        String.Format(
                            "Invalid array length {0}", value.Length));
                }

                Content = value[0].Value;
            }
        }
    }

    #endregion
}

.net相关问答推荐

获取Ef-Core集合的DeleteBehavior

.NET 4.5 项目未在 Visual Studio 2022 中编译

什么是提升运算符?

我在哪里可以获得线程安全的 CollectionView?

log4net 与 TraceSource

在目录中创建应用程序快捷方式

app.config 文件和 XYZ.settings 文件有什么区别?

使用多个 MemoryCache 实例

序列化私有成员数据

.net:System.Web.Mail 与 System.Net.Mail

ObservableCollection<> 与 List<>

使用 XmlSerializer 将空 xml 属性值反序列化为可为空的 int 属性

解密 .NET clr20r3 异常参数 P1..P10

单元测试 C# 保护方法

资源(.resx)文件有什么好处?

是否可以完全用托管的 .NET 语言编写 JIT 编译器(本地代码)

CI服务器的比较?

DLL 中有什么以及它是如何工作的?

如何将 .NET 应用程序编译为本机代码?

将控制台输出镜像到文件