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 7863973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:38:59+00:00 2026-06-02T23:38:59+00:00

Is there an easy way to construct class from a XML. The constructed class

  • 0

Is there an easy way to construct class from a XML. The constructed class will be used to serialize and deserialize XML.

I have an XML with lots of properties and elements defined. Do I need to manually create my class based on that XML? Or Is there a utility tool available to generate class from XML

Thanks,

Esen

  • 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-06-02T23:39:00+00:00Added an answer on June 2, 2026 at 11:39 pm

    @Willem van Rumpt: solution helped me to generate class. But in some case when I try to instantiate the dataset, I end up receiving this exception “Same Table cannot be the child table in two nested relations…”

    I have tried different solution using xmldocument object to navigate each nodes and generate my class that can be used to serialize and deserialize xml file. Thought to post it here so that it would be helpful to someone who is looking for similar solution. Please post your optimized solution if you have one.

    namespace Utility1 
    {
    public static class XMLHelper
    {
       private enum XMLType
       {
          Element,
          Attribute
       }
        public static string GenerateXMLClass(string xmlstring)
        {
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(xmlstring);
            XmlNode rootNode = xd.DocumentElement;
            var xmlClassCollection = new Dictionary<string, XMLClass>();
            var xmlClass = new XMLClass();
            xmlClassCollection.Add(rootNode.Name, xmlClass);
            CollectAttributes(ref xmlClass, rootNode);
            CollectElements(ref xmlClass, rootNode);
            CollectChildClass(ref xmlClassCollection, rootNode);
    
            var clsBuilder = new StringBuilder();
    
            clsBuilder.AppendLine("[XmlRoot(\"" + rootNode.Name + "\")]");
    
            foreach (var cls in xmlClassCollection)
            {
                clsBuilder.AppendLine("public class " + cls.Key);
                clsBuilder.AppendLine("{");
    
                foreach (var element in cls.Value.Elements)
                {
                    if (XMLType.Element == element.XmlType)
                        clsBuilder.AppendLine("[XmlElement(\"" + element.Name + "\")]");
                    else
                        clsBuilder.AppendLine("[XmlAttribute(\"" + element.Name + "\")]");
                    clsBuilder.AppendLine("public " + element.Type + element.Name + "{get;set;}");
                }
    
                clsBuilder.AppendLine("}");
            }
    
            return clsBuilder.ToString();
        }
    
        private static void CollectAttributes(ref XMLClass xmlClass, XmlNode node)
        {
            if (null != node.Attributes)
            {
                foreach (XmlAttribute attr in node.Attributes)
                {
                    if (null == xmlClass.Elements.SingleOrDefault(o => o.Name == attr.Name))
                        xmlClass.Elements.Add(new Element("string ", attr.Name, XMLType.Attribute));
                }
            }
        }
    
        private static bool IsEndElement(XmlNode node)
        {
            if ((null == node.Attributes || node.Attributes.Count <= 0) &&
                       (null == node.ChildNodes || !node.HasChildNodes || (node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text)))
            {
                return true;
            }
            return false;
        }
    
        private static void CollectElements(ref XMLClass xmlClass, XmlNode node)
        {
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (null == xmlClass.Elements.SingleOrDefault(o => o.Name == childNode.Name))
                {
                    var occurance = node.ChildNodes.Cast<XmlNode>().Where(o => o.Name == childNode.Name).Count();
                    var appender = "  ";
                    if (occurance > 1)
                        appender = "[] ";
    
                   if(IsEndElement(childNode))
                    {
                        xmlClass.Elements.Add(new Element("string" + appender, childNode.Name, XMLType.Element));
                    }
                    else
                    {
                        xmlClass.Elements.Add(new Element(childNode.Name + appender, childNode.Name, XMLType.Element));
                    }
                }
            }
        }
    
        private static void CollectChildClass(ref Dictionary<string, XMLClass> xmlClsCollection, XmlNode node)
        {
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (!IsEndElement(childNode))
                {
                    XMLClass xmlClass;
                    if (xmlClsCollection.ContainsKey(childNode.Name))
                        xmlClass = xmlClsCollection[childNode.Name];
                    else
                    {
                        xmlClass = new XMLClass();
                        xmlClsCollection.Add(childNode.Name, xmlClass);
                    }
                    CollectAttributes(ref xmlClass, childNode);
                    CollectElements(ref xmlClass, childNode);
                    CollectChildClass(ref xmlClsCollection, childNode);
                }
            }
        }
    
        private class XMLClass
        {
            public XMLClass()
            {
                Elements = new List<Element>();
            }
            public List<Element> Elements { get; set; }
        }
    
        private class Element
        {
            public Element(string type, string name, XMLType xmltype)
            {
                Type = type;
                Name = name;
                XmlType = xmltype;
            }
            public XMLType XmlType { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
        }
      }
    }
    

    thanks,

    Esen

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to set the volume from managed .net code?
Is there an easy way to read a single char from the console as
Is there an easy way to check in a unit test that two arrays
Is there an easy way to detect when a .NET app gets or loses
Is there an easy way to use DirectX in Java? In particular, DirectX's video
Is there an easy way to design a website to facilitate an iphone user
Is there any easy way to automatically deploy a web service / java web
Is there an easy way to create a multiline string literal in C#? Here's
Is there an easy way, when running a Ruby script, to get it to
Is there an easy way of dynamically building a filepath in .Net? At the

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.