Pedram
Pedram

Reputation: 728

Generating HTML file using XML file in C# windows form

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>

Upvotes: 1

Views: 2218

Answers (2)

KF2
KF2

Reputation: 10153

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

Upvotes: 1

w5l
w5l

Reputation: 5746

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.

Upvotes: 1

Related Questions