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