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;

               }

               }
snowp
  • 495
  • 1
  • 6
  • 22
Pedram
  • 728
  • 3
  • 10
  • 29

1 Answers1

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.

Community
  • 1
  • 1
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • Thank you @Martin but I do not want to convert my XML to HTML. – Pedram Apr 27 '13 at 10:51
  • 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.) – Martin Mulder Apr 27 '13 at 10:51