【问题标题】:Generating HTML file using XML file in C# windows form在 C# windows 窗体中使用 XML 文件生成 HTML 文件
【发布时间】:2013-04-08 11:45:54
【问题描述】:

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

【问题讨论】:

  • 我唯一遇到的问题是,当我的 XML 中有多个标签时,该类只生成一个标签的代码。
  • @IRSOG:不知道你能不能告诉我应该把这行放在哪里:htmlstring += "" + 名称 + "" + 类型 + ">";

标签: c# html xml


【解决方案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

【讨论】:

  • 谢谢 IRSOG,T=这正是我想要的。
【解决方案2】:

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

这将获取您所有的&lt;Item&gt; 标签,读取它们的 3 个属性,并为它们创建一个新行。如果您打算以这种方式创建大型文档,建议您将 string htmlstring; 替换为 StringBuilder,以提高性能。

【讨论】:

  • 我试过了,但是这样它会为 xml 文件中的每个单词生成标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 2011-06-04
  • 2011-11-29
  • 2011-10-05
  • 2014-04-03
相关资源
最近更新 更多