1

我正在尝试生成可以从 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>
4

2 回答 2

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 个属性,并为它们创建一个新行。如果您打算以这种方式创建大型文档,建议将您的替换string htmlstring;StringBuilder, 以提高性能。

于 2013-04-16T07:14:27.130 回答
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

于 2013-04-16T07:52:46.163 回答