我在C#Windows窗体中遇到xml和treeview控件的问题。
我的申请:
首先,我使用cmd命令创建一个字符串。这是xml格式的输出。 final_output是我的XMLReader的字符串。
string command =“\”C:\ lmxendutil.exe \“-licstatxml -host lwserv005 -port 6200”;
string output = ExecuteCommand(command);
string final_output = output.Substring(90, output.Length - 90);
其次我使用XMLReader和StringReader读取此字符串并在xml中解析它。我使用名为“FEATURE”的所有节点制作一个带有属性NAME =“...”的集合。
XmlReader xr = XmlReader.Create(new StringReader(final_output));
while (xr.Read())
{
switch (xr.Name)
{
case "FEATURE":
if (xr.HasAttributes)
{
while (xr.MoveToNextAttribute())
{
if (xr.Name == "NAME")
{
liste.Add(xr.Value);
}
}
}
break;
}
}
现在我在TreeView中显示我的列表。
TreeNode节点;
foreach (string x in liste)
{
node = new TreeNode(x);
treeLic.Nodes.Add(node);
}
XML结构是这样的:
<FEATURE NAME="" VERSION="" .. >
<USER NAME="" HOST="" ...>
<USER NAME="" HOST="" ...>
<USER NAME="" HOST="" ...>
<FEATURE NAME="" VERSION="" .. >
<FEATURE NAME="" VERSION="" .. >
<FEATURE NAME="" VERSION="" .. >
<USER NAME="" HOST="" ...>
<USER NAME="" HOST="" ...>
<USER NAME="" HOST="" ...>
...
我想为我的treeview获取此结构,其中包含FEATURE和USER的属性NAME的值。我的树视图只显示FEATURE with NAME属性Value。我尝试使用readsubtree,但它不起作用:(。
我的问题现在......
如何在树视图中显示具有属性NAME的alle节点,如果我单击树视图节点,我可以创建一个事件,但是如何使用它来显示有关FEATURE节点的子树节点的更多信息?
xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<LM-X STAT_VERSION="3.32">
<LICENSE_PATH TYPE="NETWORK" HOST="6200@serv005" SERVER_VERSION="4.4.4" UPTIME="53 day(s) 21 hour(s) 10 min(s) 50 sec(s)">
<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="111720" TOTAL_LICENSES="147000" SHARE="CUSTOM ,VIRTUAL">
<USER NAME="SYSTEM" HOST="SERV171" IP="172.16.11.115" USED_LICENSES="2000" LOGIN_TIME="2013-04-17 12:42" CHECKOUT_TIME="2013-04-17 12:42" SHARE_CUSTOM="hweuser:172.16.11.115"/>
>
<USER NAME="pbsadmin" HOST="LWSERV171" IP="172.16.11.115" USED_LICENSES="720" LOGIN_TIME="2013-04-17 12:44" CHECKOUT_TIME="2013-04-17 12:44" SHARE_CUSTOM="pbsadmin:LWSERV171:1592_40960072_1356792762_826820"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 07:52" CHECKOUT_TIME="2013-05-21 07:52" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 07:54" CHECKOUT_TIME="2013-05-21 07:54" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:15" CHECKOUT_TIME="2013-05-21 08:15" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:18" CHECKOUT_TIME="2013-05-21 08:18" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:23" CHECKOUT_TIME="2013-05-21 08:23" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:36" CHECKOUT_TIME="2013-05-21 08:36" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 08:37" CHECKOUT_TIME="2013-05-21 08:37" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="hassan.a" HOST="DBG5199" IP="172.16.225.114" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 09:24" CHECKOUT_TIME="2013-05-21 09:24" SHARE_CUSTOM="hassan.a:DBG5199"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 09:33" CHECKOUT_TIME="2013-05-21 09:33" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 09:33" CHECKOUT_TIME="2013-05-21 09:33" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="2000" LOGIN_TIME="2013-05-21 09:39" CHECKOUT_TIME="2013-05-21 09:39" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 10:05" CHECKOUT_TIME="2013-05-21 10:05" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="25000" LOGIN_TIME="2013-05-21 10:25" CHECKOUT_TIME="2013-05-21 10:25" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 11:27" CHECKOUT_TIME="2013-05-21 11:27" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="2000" LOGIN_TIME="2013-05-21 11:48" CHECKOUT_TIME="2013-05-21 11:48" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 11:49" CHECKOUT_TIME="2013-05-21 11:49" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
</FEATURE>
<FEATURE NAME="HWAIFPBS" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAWPF" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
答案 0 :(得分:1)
使用LinqToXml
及以下xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<FEATURE NAME="f1">
<USER NAME="u1"/>
<USER NAME="u2"/>
</FEATURE>
<FEATURE NAME="f2">
<USER NAME="u3"/>
<USER NAME="u4"/>
</FEATURE>
</Root>
var xDoc = XDocument.Load(filename);
TreeNode root = new TreeNode();
LoadTree(xDoc.Root.Element("LICENSE_PATH"), root);
treeLic.Nodes.Add(root);
void LoadTree(XElement root,TreeNode rootNode)
{
foreach (var e in root.Elements().Where(e => e.Attribute("NAME") != null))
{
var node = new TreeNode(e.Attribute("NAME").Value);
rootNode.Nodes.Add(node);
LoadTree(e, node);
}
}
答案 1 :(得分:1)
TreeNode rootNode = new TreeNode();
XMLDocument doc = new XmlDocument();
doc.LoadXml(final_Output);
foreach(XmlNode featureNode in doc.DocumentElement.SelectNodes("FEATURE[@NAME]"))
{
TreeNode fnode = new TreeNode(featureNode.Attributes["NAME"].Value);
foreach(XmlNode userNode in featureNode.SelectNodes("USER[@Name]"))
{
fNode.Nodes.Add(new TreeNode(userNode.Attributes["NAME"].Value));
}
rootNode.Nodes.Add(fnode);
}
myTreeView.Nodes.Add(rootNode);
将是另一种方式。 NB我假设xml中没有名称空间
答案 2 :(得分:0)
那是我的方法!
XmlDocument xdoc = new XmlDocument();
xdoc.Load(ofd.OpenFile());
tv1.Nodes.Clear();
AddTreeViewChildNodes(tv1.Nodes, xdoc.DocumentElement);
MessageBox.Show(ofd.FileName, "filepath")
foreach (XmlNode child_node in xml_node.ChildNodes)
{
TreeNode new_node = parent_nodes.Add(child_node.Value);
new_node = parent_nodes.Add(child_node.Name);
AddTreeViewChildNodes(new_node.Nodes, child_node);
if (new_node.Nodes.Count == 0) new_node.EnsureVisible();
}
在这里您可以找到扩展名!
custom[] customfield = new custom[0];
public Frm()
InitializeComponent();
private void c()
{
int cu = 0;
string na = "";
string fi = "";
int i = 0;
dGV_output.RowCount = 1;
dGV_output.ColumnCount = 3;
dGV_output.RowHeadersVisible = false;
dGV_output.ColumnHeadersVisible = false;
dGV_output.Rows.Add("customernumber", "name", "firsname");
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "Texts (*.xml)|*.xml|all files (*.*)|*.*",
Title = ""
};
if (ofd.ShowDialog() == DialogResult.OK)
{
riTB_ausgabe.Clear();
XmlTextReader xr = new XmlTextReader(ofd.FileName);
while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Element)
{
if (xr.AttributeCount > 0)
{
while (xr.MoveToNextAttribute())
{
switch (xr.Name)
{
case "customernumber":
ku = Convert.ToInt32(xr.Value);
break;
case "name":
na = xr.Value;
break;
case "firsname":
vo = xr.Value;
break;
}
riTB_ausgabe.Text +=
xr.Name + " -> " + xr.Value + "\n";
}
riTB_ausgabe.Text += "\n";
Array.Resize(ref customfield, customfield.Count() + 1);
customfield[i] = new custom(ku, na, vo);
i++;
dGV_ausgabe.Rows.Add(cu, na, fi);
}
}
}
xr.Close();
}
}
private void C_add(object sender, EventArgs e)
{
if(txt_cnr.Text!="" && txt_name.Text != "" && txt_firstname.Text!="")
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Xml-file(.xml)|*.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
XmlTextWriter xw = new XmlTextWriter(sfd.FileName, new UnicodeEncoding());
custom k = new custom(Convert.ToInt16(txt_cnr.Text), Convert.ToString(txt_name.Text), Convert.ToString(txt_firstname.Text));
Array.Resize(ref customfield, customfield.Count() + 1);
customfield[customfield.Count() - 1] = k;
xw.WriteStartDocument();
xw.WriteStartElement("customerlist");
foreach (Kunde k1 in customfield)
{
k1.asXmlElementWrite(xw);
}
xw.WriteEndElement();
xw.Close();
}
}
else { MessageBox.Show("error"); }
}
}