1

I am trying to generate class by which I can generate a HTML code from an XML file. It does generate the HTML code but when I have more than one tag in my XML file its value is reset.

I hope you could help me.

Here is my code in C#:

public class GenerateHTMLClass
{
    public string htmlstringbegin = "<HTML><HEADER><title>My Web-page</title></HEADER><BODY><form>\r\n";
    public string htmlstringend = "</form></BODY></HTML>";
    public string htmlstring = "";

    public GenerateHTMLClass(string xmlfileaddress, string htmlfilenaddress)
    {
        string id = "";
        string name = "";
        string type = "";
        string value = "";
        string htmlstring = "";
        XmlTextReader reader = new XmlTextReader(xmlfileaddress);            
        while (reader.Read())
        {
            switch (reader.Name)
            {
                case "GUID":
                    id = reader.ReadString();
                    break;
                case "Title":
                    name = reader.ReadString();
                    break;
                case "Type":
                    type = reader.ReadString();
                    break;
            }              
        }
        htmlstring += "<" + type + " id=" + id + " value=" + name + "/>" + name + "</" + type + ">";
        using (FileStream fs = new FileStream(htmlfilenaddress, FileMode.Create))
        {
            using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
            {
                writer.Write(htmlstringbegin + htmlstring + htmlstringend);
            }
        }
    }
}

My xml file:

<Groups>
<Group1>
    <Item1>
        <Type>Button</Type>     
        <GUID>124342345</GUID>
        <Title>Name</Title>
    </Item1>
</Group1>
<Group2>
    <Item2>
        <Type>textarea</Type>   
        <GUID>1243dsfs42345</GUID>
        <Title>Name</Title>     
    </Item2>
</Group2>
</Groups>
2
  • The only thing I have problem with is the fact that when I have more than 1 tag in my XML,the class only generates the code for one tag. Commented Apr 16, 2013 at 7:14
  • @IRSOG:I wonder if you could tell me where I should put this line:htmlstring += "<" + type + " id=" + id + " value=" + name + "/>" + name + "</" + type + ">"; Commented Apr 16, 2013 at 7:18

2 Answers 2

1

Try this:

public void GenerateHTMLClass(string xmlfileaddress, string htmlfilenaddress)
        {
            List<string> id = new List<string>();
            List<string> name = new List<string>();
            List<string> type = new List<string>();
            List<string> value = new List<string>();
            string htmlstring = "";
            XmlTextReader reader = new XmlTextReader(xmlfileaddress);

            while (reader.Read())
            {
                switch (reader.Name)
                {
                    case "GUID":
                        id.Add(reader.ReadString());
                        break;
                    case "Title":
                        name.Add(reader.ReadString());
                        break;
                    case "Type":
                        type.Add(reader.ReadString());
                        break;
                }
            }
            int i=0;
            foreach (var item in id)
            {
                htmlstring += "<" + type[i] + " id=" + item + " value=" + name[i] + "/>" + name[i] + "</" + type[i] + ">";
                i++;
            }
            using (FileStream fs = new FileStream(htmlfilenaddress, FileMode.Create))
            {
                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
                {
                    writer.Write(htmlstringbegin + htmlstring + htmlstringend);
                }
            }
        }

But better use XElement class

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

Comments

1

Try putting the HTML addition code

 htmlstring += "<" + type + " id=" + id + " value=" + name + "/>" + name + "</" + type + ">";

into the while loop. This way it will append for every tag, rather than just once after reading all tags.


EDIT: It's slightly more complex, sorry. Change your XML like so:

<Groups>
    <Group>
        <Item>
            <Type>Button</Type>     
            <GUID>124342345</GUID>
            <Title>Name</Title>
        </Item>
    </Group>
    <Group>
        <Item>
            <Type>textarea</Type>   
            <GUID>1243dsfs42345</GUID>
            <Title>Name</Title>     
        </Item>
    </Group>
</Groups>

Then load it, create a new line for each Item tag. I've used the XElement parser rather than XmlTextReader for ease of use.

var reader = XElement.Load(xmlfileaddress);

foreach (var item in reader.Descendants("Item"))
{
    var id = item.Element("GUID").Value;
    var name = item.Element("Title").Value;
    var type = item.Element("Type").Value;

    htmlstring += "<" + type + " id=" + id + " value=" + name + "/>" + name + "</" + type + ">";    
}

This will get all your <Item> tags, read their 3 properties, and create a new line for them. If you plan on creating large documents this way, it's advisable to replace your string htmlstring; with a StringBuilder, for performance.

1 Comment

I tried that,but this way it generates tags for every single word in the xml file

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.