我尝试将我的XML文件加载到树视图中,但我没有这样做。只有我得到元素名称。我希望我的树视图加载XML属性。我的问题是如何获取XML属性来加载我的树视图?
亲切的问候,
主要表格 这是初始化XML文件的表单。
public Form1()
{
InitializeComponent();
// Create an instance of the open file dialog box.
// This is test purpose only. In production xml files will come from SQL Database.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Title = "Please Choose XML File";
openFileDialog1.Filter = "XML Files (.xml)|*.xml|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;// varsayılan olarak jpg uzantıları göster
openFileDialog1.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
openFileDialog1.ShowDialog();
XmlDataDocument xmldoc = new XmlDataDocument();
XmlNode xmlnode;
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.ChildNodes[1];
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
TreeNode tNode;
tNode = treeView1.Nodes[0];
AddNode(xmlnode, tNode);
}
XML LOAD 这是我的xml加载子。
// XML NODE: ADD
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
这是XML代码:
<?xml version="1.0" encoding="utf-8"?>
<Menu>
<AgencyType id="1" name="WATER" Active="Y">
<AgencyCode id="1" name="FRESH" Active="Y">
<TypeOfBills id="1" name="INTKON" Active="Y">
<PaymentType id="1" name="AA" Active="Y"></PaymentType>
<PaymentType id="2" name="BB" Active="N" /></PaymentType>
<PaymentType id="3" name="CC" Active="N"></PaymentType>
</TypeOfBills>
</AgencyCode>
</AgencyType>
</Menu>
ORGINAL XML文件 这是我构建我的xml文件的“XML Notepad 2007”应用程序。
TREEVIEW XML LOAD 这是我在加载我的xml数据之后在表单上显示的树视图。
答案 0 :(得分:0)
尝试这样的事情
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
//add new code here
XmlNode inXmlNode = null;
XmlAttributeCollection attributes = null;
inXmlNode.Attributes.CopyTo(atttributes);
foreach(XmlAttribute attribute in attributes)
{
string name = attribute.Name;
string value = attribute.Value;
}
}
&#13;
答案 1 :(得分:0)
谢谢。我做到了。这里是。我使用&#34; XElement&#34;并且加载速度非常快。
主要强>
using System.Xml.Linq;
public Form1()
{
InitializeComponent();
// Create an instance of the open file dialog box.
// This is test purpose only. In production xml files will come from SQL Database.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Title = "Please Choose XML File";
openFileDialog1.Filter = "XML Files (.xml)|*.xml|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
openFileDialog1.ShowDialog();
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
var data = XElement.Load(openFileDialog1.FileName);
TreeNode treeNode = treeView1.Nodes.Add("Menu");
LoadElements(data, treeNode);
//Clear ListBox items
ListBoxMain.Items.Clear();
//Load ListBox First time
foreach (TreeNode n in treeView1.Nodes)
{
ListBoxMain.Items.Add(n.Text);
}
}
加载元素
private void LoadElements(XElement xElem, TreeNode treeNode)
{
foreach (XElement element in xElem.Elements())
{
if (element.HasElements)
{
if (element.FirstAttribute != null)
{
TreeNode tempNode = treeNode.Nodes.Add(element.FirstAttribute.Value + "." + element.Attribute("name").Value);
LoadElements(element, tempNode);
}
else
{
LoadElements(element, treeNode);
}
}
else
treeNode.Nodes.Add(element.FirstAttribute.Value + "." + element.Attribute("name").Value);
}
}