C# 如何显示在.aspx网站中以编程方式生成的表

C# 如何显示在.aspx网站中以编程方式生成的表,c#,asp.net,xmltable,expando,C#,Asp.net,Xmltable,Expando,我在显示以编程方式生成的表时遇到问题。让我解释一下。 我有一个XML,如下所示: <?xml version="1.0" encoding="utf-8" ?> <Fragebogen> <Header>Header</Header> <Tables> <Table> <Rows> <Row> <Cells>

我在显示以编程方式生成的表时遇到问题。让我解释一下。 我有一个XML,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<Fragebogen>
  <Header>Header</Header>
  <Tables>
    <Table>
      <Rows>
        <Row>
          <Cells>
            <Cell>Cell 1 Row 1</Cell>
          </Cells>
        </Row>
        <Row>
          <Cells>
            <Cell>Cell 1 Row 2</Cell>
          </Cells>
        </Row>
        <Row>
          <Cells>
            <Cell>Cell 1 Row 3</Cell>
            <Cell>Cell 2 Row 3</Cell>
            <Cell>Cell 3 Row 3</Cell>
          </Cells>
        </Row>
      </Rows>
    </Table>
  </Tables>
</Fragebogen>

标题
单元格1第1行
第1单元第2行
第1单元第3行
第2单元第3行
第3单元第3行
此方法:

if (String.IsNullOrWhiteSpace(file) && node == null) return null;

// If a file is not empty then load the xml and overwrite node with the
// root element of the loaded document
node = !String.IsNullOrWhiteSpace(file) ? XDocument.Load(file).Root : node;

IDictionary<String, dynamic> result = new ExpandoObject();

// implement fix as suggested by [ndinges]
var pluralizationService =
    PluralizationService.CreateService(CultureInfo.CreateSpecificCulture("en-us"));

// use parallel as we dont really care of the order of our properties
node.Elements().AsParallel().ForAll(gn =>
{
    // Determine if node is a collection container
    var isCollection = gn.HasElements &&
        (
        // if multiple child elements and all the node names are the same
            gn.Elements().Count() > 1 &&
            gn.Elements().All(
                e => e.Name.LocalName.ToLower() == gn.Elements().First().Name.LocalName) ||

            // if there's only one child element then determine using the PluralizationService if
        // the pluralization of the child elements name matches the parent node. 
            gn.Name.LocalName.ToLower() == pluralizationService.Pluralize(
                gn.Elements().First().Name.LocalName).ToLower()
        );

    // If the current node is a container node then we want to skip adding
    // the container node itself, but instead we load the children elements
    // of the current node. If the current node has child elements then load
    // those child elements recursively
    var items = isCollection ? gn.Elements().ToList() : new List<XElement>() { gn };

    var values = new List<dynamic>();

    // use parallel as we dont really care of the order of our properties
    // and it will help processing larger XMLs
    items.AsParallel().ForAll(i => values.Add((i.HasElements) ?
       GetExpandoFromXml(null, i) : i.Value.Trim()));

    // Add the object name + value or value collection to the dictionary
    result[gn.Name.LocalName] = isCollection ? values : values.FirstOrDefault();
});
return result;
if(String.IsNullOrWhiteSpace(file)&&node==null)返回null;
//如果文件不是空的,则加载xml并用
//加载文档的根元素
节点=!String.IsNullOrWhiteSpace(文件)?加载(文件).Root:节点;
IDictionary result=新的ExpandooObject();
//按照[ndinges]的建议实施修复
var多元化服务=
PluralizationService.CreateService(CultureInfo.CreateSpecificCulture(“en-us”);
//使用parallel,因为我们并不真正关心属性的顺序
node.Elements().AsParallel().ForAll(gn=>
{
//确定节点是否为集合容器
var isCollection=gn.HasElements&&
(
//如果多个子元素和所有节点名称相同
gn.Elements().Count()>1&&
gn.Elements().All(
e=>e.Name.LocalName.ToLower()==gn.Elements().First().Name.LocalName)||
//如果只有一个子元素,则确定在以下情况下使用PluralizationService
//子元素名称的复数形式与父节点匹配。
gn.Name.LocalName.ToLower()==pluralizationService.pluralization(
gn.Elements().First().Name.LocalName).ToLower()
);
//如果当前节点是容器节点,则我们希望跳过添加
//容器节点本身,但我们加载子元素
//如果当前节点有子元素,则加载
//这些子元素是递归的
var items=isCollection?gn.Elements().ToList():new List(){gn};
var值=新列表();
//使用parallel,因为我们并不真正关心属性的顺序
//它将有助于处理更大的XML
items.aspallel().ForAll(i=>values.Add((i.HasElements))?
GetExpandoFromXml(null,i):i.Value.Trim());
//将对象名称+值或值集合添加到字典中
结果[gn.Name.LocalName]=isCollection?值:values.FirstOrDefault();
});
返回结果;
正在将节点放入Expando对象中。有了这个对象,我想通过编程在.aspx网站中生成一个表。对于生成,我调用此方法:

dynamic fragebogen = GetExpandoFromXml(SSG.Fragebogen.Properties.Settings.Default.ConfigPath);
var header = fragebogen.Header;
var tables = fragebogen.Tables;
Table t;
var rows = tables[0].Rows;
TableRow r;
dynamic cells = null;
for (int i = 0; i < rows.Count; i++)
{
    cells = rows[i].Cells;
}
TableCell c;

foreach (var table in tables)
{
    t = new Table();
    foreach (var row in rows)
    {
        r = new TableRow();
        t.Rows.Add(r);
        foreach (var cell in cells)
        {
            c = new TableCell();
            c.Text = cell;
            r.Cells.Add(c);
        }
    }
}
dynamic fragebogen=GetExpandoFromXml(SSG.fragebogen.Properties.Settings.Default.ConfigPath);
var头=fragebogen.头;
var表格=fragebogen.表格;
表t;
var rows=表[0]。行;
表格r;
动态单元格=空;
for(int i=0;i
到目前为止一切都很好。行和单元格都以正确的值添加到表中,没有异常或任何其他情况,但该表不显示在网站中。
我真的不知道为什么没有显示任何内容,所以我希望您能够帮助我。

要显示此动态生成的表,您还需要将其添加到页面上的某个位置。在页面上放置一个占位符作为

<asp:PlaceHolder runat="server" ID="phOnme"></asp:PlaceHolder>

如果您的html构建正确,您不需要向我们展示所有代码,这只是分散注意力。你的问题是把表格放在页面上。实现这一点的许多方法之一是
Table t;
// generate the table...
// add it to page.
phOnme.Controls.Add(t);