65.9K
CodeProject 正在变化。 阅读更多。
Home

用于通过 XPathNavigator 将 XML 处理到 Lithium Tree 控件的附加类

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.81/5 (8投票s)

2005年4月15日

1分钟阅读

viewsIcon

47560

downloadIcon

1173

这个类将接收任何有效的 XML,并解析节点(包括或不包括属性),以便在 Lithium 树形布局控件中查看。

引言

在阅读并使用 NetronProject's 的“关于树形图和 XML”之后,该项目使用了 Lithium 树形布局控件,我被激励添加一个类来处理导入的 XML。该类使用 XpathNavigator 来遍历 XML。它也可以作为一个示例,供学习 .NET 类的人使用。

该类首先导入 XML,需要是有效的,然后允许用户指定是否显示属性。如果显示属性,用户可以指定将它们包含在节点文本中,或者作为子子节点。请参阅上面的图表以了解不同的视图。

ConsumeXml 是一个独立的类。以下是使用该类所需的步骤

try
{
  // Step 1 Load Xml
  ConsumeXml LithiumHighway = new ConsumeXml(Xml);
  // Step 2 Turn on or off attributes.                
  LithiumHighway.AttributesShow = checkBoxAttributesOn.Checked;
  // Step 3 Specify attributes as child nodes or within the node text.      
  LithiumHighway.AttributesAsChildren = checkBoxAttributesIncluded.Checked; 
  // Step 4 Process to the control. // Step 4 Process to the control.
  LithiumHighway.Process(this.lithiumControl.Root);                        

  this.lithiumControl.Root.Move(new Point(20-this.lithiumControl.Root.X, 
    Convert.ToInt32(this.lithiumControl.Height/2)-this.lithiumControl.Root.Y));

  // See the Lithium Example for this Node level highlighter, 
  // otherwise comment out.            
  ColoringVisitor visitor = new ColoringVisitor(); 

  lithiumControl.BreadthFirstTraversal(visitor);

}
catch (System.Exception ex)
{
  MessageBox.Show("Exception Caught: " + ex.Message);
}

这是在示例中使用的 XML,它混合了属性和数据节点

<?xml version='1.0' encoding='utf-8' ?>
<Top CodeProject='online'>
   <Child index='1'>Child data</Child>
   <Other Show='f'>Other Data</Other>
   <Final attr1='This' attr2='is' attr3='Radio' attr4='Clash'/>
</Top>

ConsumeXml 类处理遍历和其他项目,并通过 *木马* 将属性插入节点来显示节点。感谢在节点文本后插入了一个 System.Environment.NewLine

我的目标是快速导入 XML,这似乎是主项目中缺乏的功能,以便查看这些数据而不是编辑它们。因此,在查看之后,您的使用情况可能会有所不同。测试项目需要在编译正确之前包含 Lithium 项目。因此,首先安装该项目,然后将 Zip 文件中的 *Side Project* 添加到 Lithium 的解决方案中,然后构建。

© . All rights reserved.