locked
There was an error reflecting type for duplicate element under same parent RRS feed

  • Question

  • User-1256377279 posted

    Hi Guys,

    I am getting the error There was an error reflecting type when i try to add same element under same parent, as per my requirement i want same name.

     
      [XmlArray("CorporationNames")]
            [XmlArrayItem("CorporationName")]
            public IAMSCorporationNameCV[] FullCorporationName { get; set; }
    
    
    
    
    [XmlType("CorporationName")]
        public class IAMSCorporationNameCV
        {
    
            public IAMSCorporationNameCV()
            {
    
            }
    
            [XmlElement(ElementName = "CorporateName")]
            public string CorporateName { get; set; }
    
            [XmlElement(ElementName = "Jurisdiction")]
            public string Jurisdiction { get; set; }
    
            [XmlElement(ElementName = "AdditionalQualifiers")]
            public string AdditionalQualifiers { get; set; }
    
            [XmlElement(ElementName = "StartDate")]
            public string StartDate { get; set; }
    
            [XmlElement(ElementName = "DateRange")]
            public string DateRange { get; set; }
    
            [XmlElement(ElementName = "EndDate")]
            public string EndDate { get; set; }
    
            [XmlElement(ElementName = "NameType")]
            public string NameType { get; set; }
    
            [XmlElement(ElementName = "NameLanguage")]
            public string NameLanguage { get; set; }
    
            [XmlElement(ElementName = "NameScript")]
            public string NameScript { get; set; }
    
            [XmlElement(ElementName = "OtherIdentifierJurisdiction")]
            public string Identifier { get; set; }
    
            [XmlElement(ElementName = "OtherIdentifierJurisdiction")]
            public string IdentifierJuridictionScheme { get; set; }
    
        }

    An Expected output

    <CorporationNames>
        <CorporationName>
          <CorporateName>New data migrator XOB test 1</CorporateName>
          <Jurisdiction>London</Jurisdiction>
          <AdditionalQualifiers>Test</AdditionalQualifiers>
          <StartDate>2019</StartDate>
          <DateRange>2019</DateRange>
          <EndDate>2019</EndDate>
          <NameType>Authorised</NameType>
          <NameLanguage>English</NameLanguage>
          <NameScript>Latin</NameScript>
          <OtherIdentifierJurisdiction>Test 1  |  Test 1</OtherIdentifierJurisdiction>
          <OtherIdentifierJurisdiction>Test 2 | Test 2</OtherIdentifierJurisdiction>
        </CorporationName>    
      </CorporationNames>

    above is the result i want to achieve, i can achieve only change the case of element name, but i don't want to

    Many thanks,

    Shabbir

    Tuesday, April 30, 2019 2:59 PM

Answers

  • User-1174608757 posted

    Hi shabbir_215,

    According  to your description, you could  add order  in the ElementName ,so you could use the same name for  XmlElement. Here is demo ,I hope it could help you.

    program.cs:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApp2
    {
       
    
        [XmlType("Employee")]
        public class Employee
        {
            [XmlElement("EmpId", Order = 1)]
            public int Id { get; set; }
    
            [XmlElement("Name", Order = 2)]
            public string Name { get; set; }
    
            [XmlElement(ElementName = "OtherIdentifierJurisdiction", Order = 3)]
            public string Identifier { get; set; }
    
            [XmlElement(ElementName = "OtherIdentifierJurisdiction", Order = 4)]
            public string IdentifierJuridictionScheme { get; set; }
    
            public Employee() { }
            public Employee(string home, string work)
            {
                Identifier = "Test 1  |  Test 1";
    
                IdentifierJuridictionScheme = "Test 1  |  Test 1";
               
            }
    
            public static List<Employee> SampleData()
            {
                return new List<Employee>()
        {
            new Employee("h1","w1"){
                Id   = 1,
                Name = "pierwszy",
            },
           
        };
            }
        }
        class Program
        {
           
            static void Main(string[] args)
            {
                var employees = Employee.SampleData();
    
                System.Xml.Serialization.XmlSerializer x =
                new System.Xml.Serialization.XmlSerializer(employees.GetType());
    
                x.Serialize(Console.Out, employees);
                Console.ReadKey();
            }
    
    
        }
    }
    

    You could see:

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 1, 2019 5:38 AM

All replies

  • User-1174608757 posted

    Hi shabbir_215,

    According  to your description, you could  add order  in the ElementName ,so you could use the same name for  XmlElement. Here is demo ,I hope it could help you.

    program.cs:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApp2
    {
       
    
        [XmlType("Employee")]
        public class Employee
        {
            [XmlElement("EmpId", Order = 1)]
            public int Id { get; set; }
    
            [XmlElement("Name", Order = 2)]
            public string Name { get; set; }
    
            [XmlElement(ElementName = "OtherIdentifierJurisdiction", Order = 3)]
            public string Identifier { get; set; }
    
            [XmlElement(ElementName = "OtherIdentifierJurisdiction", Order = 4)]
            public string IdentifierJuridictionScheme { get; set; }
    
            public Employee() { }
            public Employee(string home, string work)
            {
                Identifier = "Test 1  |  Test 1";
    
                IdentifierJuridictionScheme = "Test 1  |  Test 1";
               
            }
    
            public static List<Employee> SampleData()
            {
                return new List<Employee>()
        {
            new Employee("h1","w1"){
                Id   = 1,
                Name = "pierwszy",
            },
           
        };
            }
        }
        class Program
        {
           
            static void Main(string[] args)
            {
                var employees = Employee.SampleData();
    
                System.Xml.Serialization.XmlSerializer x =
                new System.Xml.Serialization.XmlSerializer(employees.GetType());
    
                x.Serialize(Console.Out, employees);
                Console.ReadKey();
            }
    
    
        }
    }
    

    You could see:

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 1, 2019 5:38 AM
  • User-1256377279 posted

    Brilliant, Thank you very much !!!!!!!!!!!!!!!!!!! 

    Wednesday, May 1, 2019 8:46 AM