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

标签 c# html html-parsing

当我的 HTML 中的“Select”元素在我自己的浏览器中的 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/

相关文章:

php - 寻找可靠的 HTML DOM 方法来正确提取包含单个撇号的属性的文本值

c# - 使用 MSBuildWorkspace 加载解决方案和项目时出现诊断错误

c# - Linq - OrderBy 使用 lambda 表达式的两列的最小值?

c# - 查明 IEnumerable<> 是否具有唯一值的最佳方法

javascript - 为什么字段值在提交时被发布两次

html - 使用 bootstrap 对齐元素

html - CSS !important 和内联样式被覆盖

android - 如何检查在android中的Jsoup html解析器中是否存在标签

html - 从 HTML (Delphi) 中获取呈现的文本

c# - 将较小的数组插入较大的数组,反之亦然