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

  • Home
  • SEARCH
  • 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 6720047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:09:55+00:00 2026-05-26T09:09:55+00:00

I have some XML in the following schema: <Form ID=1 Formtitle=Title> <Fields> <Fieldset Legend=LegendText

  • 0

I have some XML in the following schema:

<Form ID="1" Formtitle="Title">
    <Fields>
            <Fieldset Legend="LegendText" >
                <Field FieldName="Field1" Label="Title" Type="Text" Required="1" />
                <Field FieldName="Field2" Label="Radio" Type="Radio" Required="0">
                    <Option Value="1" Text="Just One"/>
                    <Option Value="2" Text="Maybe Two"/>
                </Field>
            </Fieldset>
    </Fields>
</Form>

I need to parse through this in C# to generate a HTML form that would represent the following:

<h1>Formtitle</h1>
<form id="1" action="myurl.com">
    <fieldset>
        <legend>LegendText</legend>
        <label>Title</label>
        <input type="text" name="Field1" class="jqueryValidate"/>
        <!-- jqueryvalidate class added as required is equal to 1 in XML -->
        <label>Radio</label>
        <input type="radio" name="Field2" Value="1"/> Just One
        <input type="radio" name="Field2" Value="2"/> Maybe Two
    </fieldset>
</form>

Now, I am aware that I can achieve the same kind of thing using XSLT, however I must use C# here as I will be wrapping this up into a control that I can drop in to any of my pages.

My question is, how could I acheive something like this? I envisage it requiring some type of nested switch statements to check node names and types etc, and stringbuilding the HTML. But, I’m hoping this isn’t the case, and you boffins can help point me in the right direction with this.

Thanks in advance 🙂

Dave

  • 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-26T09:09:55+00:00Added an answer on May 26, 2026 at 9:09 am

    One way to achieve this would be to devise some objects that represent the various HTML entities that you want to generate. Note, this is a very, very simplified example. You could have a base class with all kinds of trickery and reusability in it. But that’s out of the scope of the question and we’d both be here all day 🙂

    public interface IElement
    {
        string RenderStart();
        string RenderEnd();
        string Render();
        IList<IElement> Children { get; }
        void LoadFromXML(XmLReader reader);
    }  // eo interface IElement
    
    
    public abstract class Element : IElement
    {
        List<IElement> children_ = new List<IElement>();
    
        public List<IElement> Children { get { return children_; } }
    
        public string Render()
        {
            StringBuilder builder = new StringBuilder(RenderStartTag());
            foreach(IElement e in children_)
                builder.Append(e.Render());
            builder.Append(RenderEndTag());
            return builder.ToString();
        }
    }  // eo class Element
    
    
    public class FieldSetElement : Element
    {
        public string RenderStart()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<fieldset legend=\"")
                   .Append("\">");
            return builder.ToString();
        }
    
        public string RenderEnd()
        {
            return ("</fieldset>");
        }
    
        public string Legend {get; set; }
    
        public void LoadFromXml(XmlReader reader)
        {
            Legend = reader.GetAttribute("legend");
        } // eo LoadFromXml
    }  // eo class FieldSet
    

    Obviously, you’d have an Element derived class for each HTML element. We also have a way of having them initialised from an Xml node. But now, we need to associate our Xml tags with our DOM objects. We can do that using a dictionary (in this simple case. I’d prefer a full blown factory, but again – that’s outside the scope of this answer!)

    public delegate IElement ElementCreator;
    Dictionary<string, ElementCreator> creators_ = new Dictionary<string, ElementCreator>
    

    I’ll go ahead and add the one for our FieldSet element:

    creators_["fieldset"] = () => { return new FieldSet(); };
    // and so on for other creators and elements.
    

    Ok! We’re almost there. Next step is to to translate that XML document in to objects. I’ll presume at this point we’d have some kind of a root object. I’m going to call that Html (surprising name!). As you can guess, it also derives from Element. It’s going to act as our root node.

    Html root = new Html();
    

    Now, we’ll need a function to read the Xml in and translate it in to our current parent.

    XmlReader reader = new XmlReader("layout.xml");
    Stack<IElement> currentRoot = new Stack<IElement>();
    currentRoot.Push(root);
    while(reader.Read())
    {
        // get the element tag
        if(reader.NodeType == XmlNodeType.Element)
        {
           Debug.Assert(creators_.ContainsKey(reader.Name));  // must have it!
           IElement newElement = creators_[reader.Name].Invoke(); // create it
           newElement.LoadFromXml(reader);            // tell element to read itself in
           currentRoot.Peek().Children.Add(newElement); // add to parent
           currentRoot.Push(newElement);  // we are now new parent
        }
        else if(reader.NodeType == XmlNodeType.EndElement)
           currentRoot.Pop();  // just pop it off!
    }
    

    Wow! We’re practically done. At this point, we have a collection of objects! All that remains is to render them.

    string html = root.Render();
    

    BAM. We’re done.

    Sorry if there are any errors in the code, I’ll try and go over it. I have not run this, but I have done something similar before. I’m also not suggesting this is the best way, I am sure there are others. This is one way. I’ve left out the rendering of attributes for reasons of brevity, but I’ll be happy to add another concrete Element example to show how that might work.

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

Sidebar

Related Questions

So I have some XML in the following format: <somenode> <html xmlns=http://www.w3.org/1999/xhtml> <head> <title/>
I have some xml like this: <Data> <Rows> <Row> <Field Name=title>Mr</Field> <Field Name=surname>Doe</Field> <Row>
If I have some xml containing things like the following mediawiki markup: ...collected in
I have the following output (via link) which displays the var_dump of some XML
I am using SAX to parse some XML. Let's say I have the following
I have the following start of an XSD: <?xml version=1.0 encoding=UTF-8?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns:no=http://www.sychophants.com>
I have a following XML file: <titles> <book title=XML Today author=David Perry/> <book title=XML
Suppose I have a XML schema and want to support some extensions at several
Suppose I have the following XML, how should I update my XSD schema (described
I have the following xml which has xsd schema, but is poor and no

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.