Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:22:12+00:00 2026-05-30T01:22:12+00:00

I have the following xml which represents 2 types of plugins, FilePlugin and RegsitryPlugin:

  • 0

I have the following xml which represents 2 types of plugins, FilePlugin and RegsitryPlugin:

<Client>
  <Plugin Type="FilePlugin">
    <Message>i am a file plugin</Message>
    <Path>c:\</Path>
  </Plugin>
  <Plugin Type="RegsitryPlugin">
    <Message>i am a registry plugin</Message>
    <Key>HKLM\Software\Microsoft</Key>
    <Name>Version</Name>
    <Value>3.5</Value>
  </Plugin>
</Client>

I would like to deserialize the xml into objects.
As you can see, the ‘Message’ element is repeating itself both for the FilePlugin and the RegistryPlugin and i am using inheritance for this:

    abstract class Plugin
    {
        private string _message;
        protected Plugin(MISSING conf)
        {
            // here i need to set my private members like:
            // Message = MISSING.Message;
        }
    }

    class FilePlugin : Plugin
    {
        private string _path;
        public FilePlugin(MISSING config)
            : base(config)
        {
            // Here i need to set my private members like:
            // _path = config.Path;
        }
    }

    class RegistryPlugin : Plugin
    {
        private string _key;
        private string _name;
        private string _value;
        public RegistryPlugin(MISSING config)
            : base(config)
        {
            // Here i need to set my private members like:
            // _key = config.Key;
            // _key = config.Name;
            // _key = config.Value;
        }
    }
}

I need somehow to deserialize the xml, and than to decide according the PluginType Element which Instance to create:
i.e:
if it is written in the xml the Type=FilePlugin than i need to create

Plugin p1 = new FilePlugin(conf);

if it is written in the xml the Type=RegistryPlugin than i need to create

Plugin p2 = new RegistryPlugin(conf);

Please follow my comments in my code in order to understand the missing parts.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:22:13+00:00Added an answer on May 30, 2026 at 1:22 am

    Creating your own deserializer isn’t also hard. Here is my solution to this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Xml.Linq;
    using System.Reflection;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        public abstract class Plugin
        {
            public string Type { get; set; }
            public string Message { get; set; }
        }
    
        public class FilePlugin : Plugin
        {
            public string Path { get; set; }
        }
    
        public class RegsitryPlugin : Plugin
        {
            public string Key { get; set; }
            public string Name { get; set; }
            public string Value { get; set; }
        }
    
        static class MyProgram
        {
            [STAThread]
            static void Main(string[] args)
            {
                string xmlstr =@"
                    <Client>
                      <Plugin Type=""FilePlugin"">
                        <Message>i am a file plugin</Message>
                        <Path>c:\</Path>
                      </Plugin>
                      <Plugin Type=""RegsitryPlugin"">
                        <Message>i am a registry plugin</Message>
                        <Key>HKLM\Software\Microsoft</Key>
                        <Name>Version</Name>
                        <Value>3.5</Value>
                      </Plugin>
                    </Client>
                  ";
    
                Assembly asm = Assembly.GetExecutingAssembly();
                XDocument xDoc = XDocument.Load(new StringReader(xmlstr));
                Plugin[]  plugins = xDoc.Descendants("Plugin")
                    .Select(plugin =>
                    {
                        string typeName = plugin.Attribute("Type").Value;
                        var type = asm.GetTypes().Where(t => t.Name == typeName).First();
                        Plugin p = Activator.CreateInstance(type) as Plugin;
                        p.Type = typeName;
                        foreach (var prop in plugin.Descendants())
                        {
                            type.GetProperty(prop.Name.LocalName).SetValue(p, prop.Value, null);
                        }
    
                        return p;
                    }).ToArray();
    
                //
                //"plugins" ready to use
                //
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,
I have a large XML file which in the middle contains the following: <ArticleName>Article
I have the following xml file which I am parsing using NSXMLParser : <geometry
I have the following XML file with page nodes which I want to read
i have the following xml: <messageContent xmlns=http://tempuri.org/ > <Message type=MappedMessage > <Properties RequestId=Point-CurveRequest-8326ad44-a1cd-4a96-b4ef-1c4ad213d940 Action=getParCurves
I have an xml document which consists of a number of the following: -
I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>
I have the following snippet in my pom.xml (Full pom attached below which can
I have the following output (via link) which displays the var_dump of some XML
I have XML in the following format which I want to reformat: <blocks> <!--

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.