本文介绍了xml到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xDocument将xml转换为html表我可以获取表但是我有一个嵌套的xml文档,并且它无法让我为表中的多个标题行添加额外的行。  ;


喜欢这样


标题


rowdata1 rowdata2 rowdata3


rowdataa rowdatab rowdatac


header2


rowdata1 rowdata2 rowdata3


rowdataa rowdatab rowdatac


header3


rowdata1 rowdata2 rowdata3


rowdataa rowdatab rowdatac



取而代之的是


header


rowdata1 rowdata2 rowdata3


rowdataa rowdatab rowdatac


rowdata1 rowdata2 rowdata3


rowdataa rowdatab rowdatac


rowdata1 rowdata2 rowdata3


rowdataa rowdatab rowdatac



xml

<模块名称= QUOT; FOO"> 
< Service id =" 1">
< Name> Check_Service_Status_foo< / Name>
....
< / Service>
< Service id =" 2">
< Name> Check_Service_Status_bar< / Name>
...
< / Service>
< Service id =" 3">
< Name> thisthat< / Name>
< / Service>
...
< / Module>
<模块名称=" foo2">
< Service id =" 4">
< Name> Check_Service_Status_foo2< / Name>
...
< / Service>
....
< / Module>

代码c#

 foreach(xmlDoc.Descendants(" Root")中的var rootdescendant)
{
var modulename = rootdescendant.Element(" ;头")属性("名称")值。
TableRow rwh = new TableRow();
TableCell cellh = new TableCell();
cellh.Text = modulename;
rwh.Cells.Add(cellh);
tbl.Controls.Add(rwh);
foreach(vardesdesndant in Rootdescendant.Descendants(" Module"))
{
foreach(vardescendant.Descendants(" Service")中的var descendant)
{
i ++;
var server = descendant.Element(" Name")。Value;
TableRow rw = new TableRow();
TableCell cell = new TableCell();
cell.Text = server;
rw.Cells.Add(cell);
}
}
}
myph.Controls.Add(tbl);

我试图将其修改为基本元素。希望我没有多少修剪。 提前谢谢你。

解决方案

I'm using xDocument to convert xml to an html table I can get the table but I have a nested xml document and it is failing to let me add an extra row for a multiple heading rows in table. 

Like so

header

rowdata1 rowdata2 rowdata3

rowdataa rowdatab rowdatac

header2

rowdata1 rowdata2 rowdata3

rowdataa rowdatab rowdatac

header3

rowdata1 rowdata2 rowdata3

rowdataa rowdatab rowdatac

Instead what I get is

header

rowdata1 rowdata2 rowdata3

rowdataa rowdatab rowdatac

rowdata1 rowdata2 rowdata3

rowdataa rowdatab rowdatac

rowdata1 rowdata2 rowdata3

rowdataa rowdatab rowdatac

xml

<Module name="foo">
  <Service id="1">
    <Name>Check_Service_Status_foo</Name>
   ....
  </Service>
  <Service id="2">
    <Name>Check_Service_Status_bar</Name>
    ...
  </Service>
  <Service id="3">
    <Name>thisthat</Name>
  </Service>
  ...
</Module>
<Module name="foo2">
  <Service id="4">
    <Name>Check_Service_Status_foo2</Name>
    ...
  </Service>
   ....
</Module>

Code c#

 foreach (var rootdescendant in xmlDoc.Descendants("Root"))
        {
            var modulename = rootdescendant.Element("Header").Attribute("name").Value;
            TableRow rwh = new TableRow();
            TableCell cellh = new TableCell();
            cellh.Text = modulename;
            rwh.Cells.Add(cellh);
            tbl.Controls.Add(rwh);
            foreach(var moddescendant in rootdescendant.Descendants("Module"))
            {
            foreach (var descendant in moddescendant.Descendants("Service"))
            {
                i++;
                var server = descendant.Element("Name").Value;
                TableRow rw = new TableRow();
                TableCell cell = new TableCell();
                cell.Text = server;
                rw.Cells.Add(cell);
            }
        }
        }
        myph.Controls.Add(tbl);

I've tried to trim this down to the basic elements. Hoping I did not trim to much.  Thank you in advance.

解决方案


这篇关于xml到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:01