将具有相同名称但不同属性的文档序列化为一个类

问题描述 投票:0回答:1

我正在尝试用一个类读取两个 Aux 文件,但遇到了麻烦。

当我尝试序列化我的类时遇到错误。 xml 架构是由第三方定义的,我无法更改它。

xml 看起来像这样

<General>
    <Projection>
        <Type>AA-A</Type>
        <Parameter>A12</Parameter>
    </Projection>
</General>

还有另一个 xml 文件

<General>
    <Projection>AA-A</Projection>
</General>

我定义的类是这样的

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class General
    {
        private GeneralProjection projectionField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public GeneralProjection Projection
        {
            get
            {
                return this.projectionField;
            }
            set
            {
                this.projectionField = value;
            }
        }
    }


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class GeneralProjection
    {

        private string typeField;

        private string parameterField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Parameter
        {
            get
            {
                return this.parameterField;
            }
            set
            {
                this.parameterField = value;
            }
        }
    }

第一个 xml 文件可读性很好,但第二个文件则不行。

请帮忙。

我尝试了以下方法,出现错误:“‘Namespace’的 XML 元素‘Projection’已存在于当前范围内。”

// GeneralProjection 타입의 Projection 처리
[System.Xml.Serialization.XmlElementAttribute("Projection", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public GeneralProjection GeneralProjection
{
    get { return this.projectionField; }
    set { this.projectionField = value; }
}

// string 타입의 Projection 처리
[System.Xml.Serialization.XmlElementAttribute("Projection", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ProjectionString
{
    get { return this.projectionStringField; }
    set { this.projectionStringField = value; }
}

c# xml serialization xml-serialization
1个回答
0
投票

处理这种情况的方法有很多。这是最简单的。

using System.Xml;
using System.Xml.Serialization;

var serializer = new XmlSerializer(typeof(General));
serializer.UnknownNode += Serializer_UnknownNode;

void Serializer_UnknownNode(object? sender, XmlNodeEventArgs e)
{
    if (e.NodeType == XmlNodeType.Text)
    {
        var projection = (Projection)e.ObjectBeingDeserialized!;
        projection.Type = e.Text!;
    }
}

using var fs = new FileStream("test.xml", FileMode.Open);

var general = (General?)serializer.Deserialize(fs);

Console.WriteLine(general?.Projection.Type ?? "<null>");
Console.WriteLine(general?.Projection.Parameter ?? "<null>");

public class General
{
    public required Projection Projection { get; set; }
}

public class Projection
{
    public required string Type { get; set; }
    public required string Parameter { get; set; }
}

订阅事件并提取价值。

此代码适用于 xml。

© www.soinside.com 2019 - 2024. All rights reserved.