0

I'm trying to read two Aux files with one class, but I'm having trouble.

I'm getting an error when I try to serialize my class. The xml schema is defined by a third-party and I can't change it.

The xml looks like this

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

And another xml file

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

The class I defined is this

/// <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;
            }
        }
    }

The first xml file reads well, but the second file cannot.

Please help.

I've tried the following methods, An error has occurred: "The XML element 'Projection' of 'Namespace' already exists in the current range."

// 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; }
}

1 Answer 1

0

There are many ways to handle such situations. Here is the simplest one.

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; }
}

Subscribe to the event and extract the value.

This code works with both xml.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.