如何使用 Webforms 和 C# 显示包含董事、经理和员工的分层组织结构图?
假设我有一份包含员工以及向他们报告的人员的 XML 文档。像这样的事情:
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee>
<id>1</id>
<name>Ashley King</name>
<title>Director</title>
<directReports>
<employee>
<id>2</id>
<name>Jim Warden</name>
<title>Manager</title>
<directReports>
<employee>
<id>3</id>
<name>Jeff Reese</name>
<title>Lead</title>
<directReports>
<employee>
<id>4</id>
<name>Leann Thompson</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>5</id>
<name>Amber Herzer</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>6</id>
<name>Ben Lively</name>
<title>Staff</title>
<directReports />
</employee>
</directReports>
</employee>
<employee>
<id>3</id>
<name>Mary Hibdon</name>
<title>Lead</title>
<directReports>
<employee>
<id>50</id>
<name>Brandon Burns</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>55</id>
<name>David Peck</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>62</id>
<name>Ben Lively</name>
<title>Staff</title>
<directReports />
</employee>
</directReports>
</employee>
</directReports>
</employee>
</directReports>
</employee>
</employees>
我想使用 webforms (c#) 根据我提供的 xml 文档中的数据创建动态组织结构图。我尝试了几种不同程度的蹩脚方法。
在我看来,解决方案必须包含一个 html 表(畏缩)。主要是因为每个员工的职位都很重要,我们不能让他们浮动在浏览器窗口中(如果您不同意,请纠正我)。顶级员工 Askley King 将成为顶部单元格,其 colspan 为 X。Jim Wardon 将成为具有相同 colspan 的下一行,因为他在该 XML 文档中没有任何同时代人。 Jeff Reese 和 Mary Hibdon 将是下一行,其跨度为 x / 2。然后,下一行有下一个员工,我开始陷入如何完成它的困境。
你会怎么做?代码或概念......没有区别。我只需要指明正确的方向。
Assume I have an XML doc with employees and who reports to them. Something like this:
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee>
<id>1</id>
<name>Ashley King</name>
<title>Director</title>
<directReports>
<employee>
<id>2</id>
<name>Jim Warden</name>
<title>Manager</title>
<directReports>
<employee>
<id>3</id>
<name>Jeff Reese</name>
<title>Lead</title>
<directReports>
<employee>
<id>4</id>
<name>Leann Thompson</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>5</id>
<name>Amber Herzer</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>6</id>
<name>Ben Lively</name>
<title>Staff</title>
<directReports />
</employee>
</directReports>
</employee>
<employee>
<id>3</id>
<name>Mary Hibdon</name>
<title>Lead</title>
<directReports>
<employee>
<id>50</id>
<name>Brandon Burns</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>55</id>
<name>David Peck</name>
<title>Staff</title>
<directReports />
</employee>
<employee>
<id>62</id>
<name>Ben Lively</name>
<title>Staff</title>
<directReports />
</employee>
</directReports>
</employee>
</directReports>
</employee>
</directReports>
</employee>
</employees>
I'd like to use webforms (c#) to create dynamic org charts based on the data in the xml doc I feed it. I have tried a few methods with varying levels of crappiness.
It seems to me like the solution will have to include an html table (cringe). Mainly because the position of each employee is significant and we can't have them floating around the browser window (please correct me if you disagree). The top employee, Askley King, would be be the top cell, a with a colspan of X. Jim Wardon would be the next row with the same colspan since he doesn't have any contemporaties in that XML doc. Jeff Reese and Mary Hibdon would be the next row with colspans of x / 2. Then, the next row has the next employees and I start to get bogged down in how to complete it.
How would you do it? Code or concept... makes no difference. I just need to be pointed in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XML 喜欢递归。将节点一一布置或布置到某个巨大的表格网格中需要做大量工作——您的数据格式是分层的,您的处理方法也应该是分层的。
XSLT 非常擅长将 xml 转换为 html。以下 xslt 将您的 xml 转换为一系列嵌套的 div 和表。我正在使用表格来使每个同事组保持一致。似乎是最简单的方法。
要预览,请从 http://pastebin.com/f6597d519 保存 xslt
添加
到员工 xml 的顶部,然后在浏览器中打开。在实践中,您可能希望在服务器上进行转换以避免兼容性问题。要在 C# 中执行此操作,请参阅如何在 C# 中应用 XSLT 样式表< /a>
XML loves recursion. It's a lot of work to lay out the nodes one-by-one or into some gigantic table grid -- your data format is heirarchical and your processing method should be as well.
XSLT is pretty good at transforming xml into html. the following xslt transforms your xml into a series of nested divs and tables. I'm using a table to get each colleague group aligned. seemed like the easiest way.
To preview, save the xslt from http://pastebin.com/f6597d519
Add
<?xml-stylesheet href="foo.xslt" type="text/xsl"?>
to the top of your employees xml, and open in a browser. In practice, you may want to transform on the server to avoid compatibility issues. To do that in C#, see How to apply an XSLT Stylesheet in C#
有一个商业网络控件,有免费版本。
http://unifosys.com/chart4.net/
来自 XML 在线演示的分层图表位于此处:
http://organization.unifosys.com/demo.aspx
There is a commercial web control, has free version.
http://unifosys.com/chart4.net/
Hierarchical chart from XML online demo is here:
http://organization.unifosys.com/demo.aspx
SmartDraw 这里有免费的组织结构图: http://www.smartdraw.com/specials/ppc/org-chart-software.htm?id=141414&gclid=CPSep4-a158CFYsz3god1VRybg
我自己没有使用过它们。
据我所知,msCharts 似乎不支持组织结构图。
SmartDraw have free org charts here: http://www.smartdraw.com/specials/ppc/org-chart-software.htm?id=141414&gclid=CPSep4-a158CFYsz3god1VRybg
I have not used them myself.
From what I can find it looks like msCharts does not support org charts.