[Fork] https://stackoverflow.com/questions/73778437/how-to-parse-xml-attribute-with-namespace-and-xml-type-in-c-sharp

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.Reflection;

using NUnit.Framework; //NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/73778437/how-to-parse-xml-attribute-with-namespace-and-xml-type-in-c-sharp

    [XmlRoot(IsNullable = false,ElementName = "IntegrationActionResponse",Namespace = "example.com")]
    public class LoadIActionResponseModel
    {
        [XmlElement("ActionResults", Namespace = "")]
        public LoadIActionResults ActionResults { get; set; }
    }

    public class LoadIActionResults
    {
        [XmlElement(ElementName = "SuccessCount", Namespace = "")]
        public int SuccessCount { get; set; }

        [XmlElement(ElementName = "FailureCount", Namespace = "")]
        public int FailureCount { get; set; }

        [XmlElement(ElementName = "ActionResult", Namespace = "")] // Fixed
        public ActionResultBase ActionResult { get; set; }
    }
    
	// Abstract base class added
	[XmlType(TypeName="ActionResult", Namespace = "example.com")] // Fixed
	[XmlInclude(typeof(ParameterizedIntegrationActionResult))] // Added
	public abstract class ActionResultBase // Added
	{
	}

	// LoadIActionResult renamed and made a subclass
	[XmlType(TypeName="ParameterizedIntegrationActionResult", Namespace = "example.com")] // Fixed
    public class ParameterizedIntegrationActionResult : ActionResultBase
    {
        [XmlElement(ElementName = "Status", Namespace = "")]
        public string Status { get; set; }

        [XmlElement(ElementName = "resultParams", Namespace = "")]
        public LoadIActionResultParams resultParams {get; set;}
    }
   
    public class LoadIActionEntry
    {
        [XmlElement(ElementName = "key")]
        public string Key { get; set; }

        [XmlElement(ElementName = "value")]
        public string Value { get; set; }
    }
  
    public class LoadIActionResultParams
    {
        [XmlElement(ElementName = "entry")]
        public List<LoadIActionEntry> Entries { get; set; }
    }

    public class TestClass
    {
        public static void Test()
        {
			var xml = GetXml();
			
			//Console.WriteLine(xml);
			
			var response = xml.LoadFromXml<LoadIActionResponseModel>();
			
			var xml2 = response.GetXml();
			
			Console.WriteLine("Re-serialized {0}:", response);
			Console.WriteLine(xml2);
		}
		
		static string GetXml() => @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<ns2:IntegrationActionResponse xmlns:ns2=""example.com"">
    <ActionResults>
        <SuccessCount>1</SuccessCount>
        <FailureCount>0</FailureCount>
        <ActionResult xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""ns2:ParameterizedIntegrationActionResult"">
            <Status>OK</Status>
            <resultParams>
                <entry>
                    <key>abc</key>
                    <value>abc</value>
                </entry>
                <entry>
                    <key>abc</key>
                    <value>abc</value>
                </entry>
                <entry>
                    <key>abc</key>
                    <value>abc</value>
                </entry>  
            </resultParams>
        </ActionResult>
    </ActionResults>
</ns2:IntegrationActionResponse>";
    }

    public static class XmlSerializationHelper
    {
        public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
        {
            serial = serial ?? new XmlSerializer(typeof(T));
            using (var reader = new StringReader(xmlString))
            {
				return (T)serial.Deserialize(reader);
            }
        }

        public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
        {
			return obj.GetXml(null, omitStandardNamespaces);
        }
		
        public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
        {
            XmlSerializerNamespaces ns = null;
            if (omitStandardNamespaces)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
            }			
            using (var textWriter = new StringWriter())
            {
                var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
                using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                    (serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
                return textWriter.ToString();
            }
        }
		
		public static string GetOuterXml(this XmlNode node, bool indent = true)
		{
			if (node == null)
				return null;
			using (var textWriter = new StringWriter())
			{
				var settings = new XmlWriterSettings
				{
					Indent = indent,
					OmitXmlDeclaration = true,
				};
				using (var xmlWriter = XmlWriter.Create(textWriter, settings))
				{
					node.WriteTo(xmlWriter);
				}
				return textWriter.ToString();
			}
		}
    }
				
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1}, {2})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;