使用C#windows窗体中的XML文件生成HTML文件

时间:2013-04-16 07:07:27

标签: c# html xml

我正在尝试生成类,通过该类我可以从XML文件生成HTML代码。它确实生成HTML代码,但是当我的XML文件中有多个标记时,它的值将被重置。

我希望你能帮助我。

这是我在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);
            }
        }
    }
}

我的xml文件:

<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 个答案:

答案 0 :(得分:1)

尝试添加HTML添加代码

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

进入while循环。这样它将附加每个标签,而不是在读取所有标签后只附加一次。


编辑:稍微复杂一些,抱歉。像这样更改XML:

<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>

然后加载它,为每个Item标记创建一个新行。我使用XElement解析器而不是XmlTextReader来方便使用。

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 + ">";    
}

这将获取所有<Item>标记,读取其3个属性,并为它们创建新行。如果您打算以这种方式创建大型文档,建议您使用StringBuilder替换string htmlstring;,以提高性能。

答案 1 :(得分:1)

试试这个:

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);
                }
            }
        }

但最好使用XElement