gpt4 book ai didi

c# - 将字符串分配给 HtmlElement 对象的 InnerHtml

转载 作者:行者123 更新时间:2023-11-28 00:20:33 26 4
gpt4 key购买 nike

我尝试在我的 HTML 中填充一个“选择”元素,而它在我自己的浏览器中的 C# 应用程序中打开。所以我这样做了:

DataTable dt = new DataTable();
HtmlElement States = webBrowser1.Document.GetElementById("Select1");
da.Fill(dt);
States.InnerHtml = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
string head = String.Format(@"<option Value=""{1}"">",
dt.Rows[i]["Code"].ToString());
string body = String.Format("{0}</option>", dt.Rows[i]["Name"].ToString());
string tag = head + body;

States.InnerHtml =tag;
}

但令人惊讶的是,当我期望结果是这样的

< option Value="aNumber">testString<\option>

我看到了这个:

testString<\option>

当我努力写出预期的字符串时,我在这里看到了同样的问题。最后,我在字符串的第二个字符中插入了一个空格。因此我写了< option>而不是 <option>在这里。我对要传递给 InnerHtml 的字符串也做了同样的事情我的元素的属性。但不幸的是,它没有用。

最佳答案

这不应该

string head = String.Format(@"<option Value=""{1}"">", 
dt.Rows[i]["Code"].ToString());

是这个吗?

string head = String.Format(@"<option Value=""{0}"">", 
dt.Rows[i]["Code"].ToString());

关于c# - 将字符串分配给 HtmlElement 对象的 InnerHtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9273179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com