1

I wanted to generate an Html code with an XML file. Here is my xml:

 <XML>
 <Groups>
<Group Name="Group1">
    <Item ID="9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167"/>
    <Item ID="351FEF76-B826-426F-88C4-DBAAA60F886B"/>
    <Item ID="96A4CBFC-04CD-4D27-ADE6-585C05E4DBC9"/>
    <Item ID="D8876943-5861-4D62-9249-C5FEF88219FA"/>
</Group>
<Group Name="Group2">
    <Item ID="9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167"/>
    <Item ID="351FEF76-B826-426F-88C4-DBAAA60F886B"/>
</Group>
 </Groups>
 <Items>
<Item>
    <GUID>9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167</GUID>
    <Type>button</Type>
    <Title>Save</Title>
    <Value>submit</Value>
    <Name>btnsave</Name>
    <MaxLen>5</MaxLen>
</Item>    
<Item>
    <GUID>351FEF76-B826-426F-88C4-DBAAA60F886B</GUID>
    <Type>text</Type>
    <Title>Name:</Title>
    <Name>txtname</Name>
    <Value>Name</Value>
    <MaxLen>2</MaxLen>
</Item>    
<Item>
    <GUID>02973DCC-5677-417C-A9BF-1578F58923EF</GUID>
    <Type>text</Type>
    <Title>Family:</Title>
    <Name>txtFamiy</Name>
    <Value>Family</Value>
    <MaxLen>2</MaxLen>
</Item> 
<Item>
    <GUID>96A4CBFC-04CD-4D27-ADE6-585C05E4DBC9</GUID>
    <Type>checkbox</Type>
    <Title>I agree to the terms.</Title>
    <Name>chkagree</Name>
    <Value>Agree</Value>
    <MaxLen>10</MaxLen>
</Item>    
    <Item>
    <GUID>D8876943-5861-4D62-9249-C5FEF88219FA</GUID>
    <Type>select</Type>
    <Title>Type of property</Title>
    <Name>PropertyType</Name>
    <Value></Value>     
</Item>    

The problem that I have is the fact that I need to create a fieldset tag for every group and the related elements must be in that fieldset.

Here is my C# code:

XmlTextReader reader = new XmlTextReader(xmlfileaddress);
      Group objGroup = new Group();
      while (reader.Read())
      {
        switch (reader.Name)
        {
           case "Groups":
           while (reader.Read())
        {
          if (reader.NodeType == XmlNodeType.EndElement)
          break;
          switch (reader.Name)
          {
            case "Group":
            if (reader.IsStartElement())
            {

            }
          while (reader.Read())
          {

            if (reader.NodeType == XmlNodeType.EndElement)
            break;
            switch (reader.Name)
            {
                case "Item":
                objGroup.ItemIDs.Add(new Guid());
                break;
            }
            }
                break;
            }
            }
                 break;

           case "Items":                        
           Item objItem = new Item();
           while (reader.Read())
           {
            if (reader.NodeType == XmlNodeType.EndElement)
            break;
           switch (reader.Name)
           {
             case "Item":
             while (reader.Read())
             {

               if (reader.NodeType == XmlNodeType.EndElement)
               break;
               switch (reader.Name)
               {
                 case "GUID":                                                    
                 objItem.Id = reader.ReadElementContentAsString();
                 break;

                 case "Title":
                 objItem.Title = reader.ReadElementContentAsString();
                 break;

                 case "Type":
                 objItem.Type = reader.ReadElementContentAsString();
                 break;

                 case "Value":
                 objItem.Value = reader.ReadElementContentAsString();
                 break;

                 case "Name":
                 objItem.Name = reader.ReadElementContentAsString();
                 break;

                 case "MaxLen":
                 objItem.MaxLen = reader.ReadElementContentAsString();
                 break;

               }

               }
3
  • Hey Pedram, I am curious to how you solved your problem. Commented Apr 28, 2013 at 15:04
  • @MartinMulder : It has not been solved yet.Thanks for asking. Commented Apr 29, 2013 at 3:59
  • I guess I need to declare classes for Item and Group,then again for form. This way I can know which control to generate Commented Apr 29, 2013 at 4:02

1 Answer 1

1

Dit you consider XSLT?

XSLT is a XML-based language to generate another GML-based file like HTML from your XML-data.

See for more information: How to apply an XSLT Stylesheet in C#

Examples of XSLT (and online converters) can be found here: http://www.w3schools.com/xsl/xsl_examples.asp You can play on that site with your XML and your XSLT before you implement it in your code with the code in the first link.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Martin but I do not want to convert my XML to HTML.
Perhapse I used to incorrect word. You will always keep your XML file. The XML is your "data file", the XSLT is your "template" and with these two you can generate the "result file". You can repeat this process as many times as you want (for example: after your xml-data has changed.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.