TreeView的问题
1.我从XML文件里读出Node,给TreeView,可是显示不出来
base.xml:
<Nodes id="AllNodes" Text="全部导入项">
<Color id="Color" Text="商品颜色">
</Color>
<Style id="Style" Text="商品样式">
</Style>
<SupNo id="SupNo" Text="厂商">
</SupNo>
<Class id="Class" Text="商品类别">
</Class>
<Largness id="Largness" Text="商品(赠品)">
</Largness>
<Product id="Product" Text="商品(主品)">
</Product>
<ColorAndStyle id="ColorAndStyle" Text="商品色样">
</ColorAndStyle>
<PandL id="PandL" Text="主赠品关系">
<Explain>说明</Explain>
</PandL>
</Nodes>
代码:
public TreeView GetLoadGuideItems()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(XMLFILEPATH);
XmlNode xnroot = xdoc.DocumentElement;
XmlNodeList xnl = xnroot.ChildNodes;
TreeView tv = new TreeView();
foreach (XmlNode xn in xnl)
{
TreeNode tn = new TreeNode();
tn.Name = xn.Attributes["id"].Value;
tn.Text = xn.Attributes["Text"].Value;
tv.Nodes.Add(tn);
}
return tv;
}
1.我从XML文件里读出Node,给TreeView,可是显示不出来
base.xml:
<Nodes id="AllNodes" Text="全部导入项">
<Color id="Color" Text="商品颜色">
</Color>
<Style id="Style" Text="商品样式">
</Style>
<SupNo id="SupNo" Text="厂商">
</SupNo>
<Class id="Class" Text="商品类别">
</Class>
<Largness id="Largness" Text="商品(赠品)">
</Largness>
<Product id="Product" Text="商品(主品)">
</Product>
<ColorAndStyle id="ColorAndStyle" Text="商品色样">
</ColorAndStyle>
<PandL id="PandL" Text="主赠品关系">
<Explain>说明</Explain>
</PandL>
</Nodes>
代码:
public TreeView GetLoadGuideItems()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(XMLFILEPATH);
XmlNode xnroot = xdoc.DocumentElement;
XmlNodeList xnl = xnroot.ChildNodes;
TreeView tv = new TreeView();
foreach (XmlNode xn in xnl)
{
TreeNode tn = new TreeNode();
tn.Name = xn.Attributes["id"].Value;
tn.Text = xn.Attributes["Text"].Value;
tv.Nodes.Add(tn);
}
return tv;
}
代码:
private void XmlWriter()
{
XmlDocument xdoc = new XmlDocument();
XmlNode BootNode = xdoc.CreateElement("Nodes");
this.AppendXmlAttribute(BootNode, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "AllNodes"), this.CreateXmlAttribute(xdoc, "Text", "全部导入项") });
#region ==== 子节点 ====
XmlNode Color = xdoc.CreateElement("Color");
this.AppendXmlAttribute(Color, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "Color"), this.CreateXmlAttribute(xdoc, "Text", "商品颜色") });
XmlNode Style = xdoc.CreateElement("Style");
this.AppendXmlAttribute(Style, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "Style"), this.CreateXmlAttribute(xdoc, "Text", "商品样式") });
XmlNode SupNo = xdoc.CreateElement("SupNo");
this.AppendXmlAttribute(SupNo, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "SupNo"), this.CreateXmlAttribute(xdoc, "Text", "厂商") });
XmlNode Class = xdoc.CreateElement("Class");
this.AppendXmlAttribute(Class, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "Class"), this.CreateXmlAttribute(xdoc, "Text", "商品类别") });
XmlNode Largness = xdoc.CreateElement("Largness");
this.AppendXmlAttribute(Largness, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "Largness"), this.CreateXmlAttribute(xdoc, "Text", "商品(赠品)") });
XmlNode Product = xdoc.CreateElement("Product");
this.AppendXmlAttribute(Product, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "Product"), this.CreateXmlAttribute(xdoc, "Text", "商品(主品)") });
XmlNode ColorAndStyle = xdoc.CreateElement("ColorAndStyle");
this.AppendXmlAttribute(ColorAndStyle, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "ColorAndStyle"), this.CreateXmlAttribute(xdoc, "Text", "商品色样") });
XmlNode PandL = xdoc.CreateElement("PandL");
this.AppendXmlAttribute(PandL, new XmlAttribute[] { this.CreateXmlAttribute(xdoc, "id", "PandL"), this.CreateXmlAttribute(xdoc, "Text", "主赠品关系") });
#endregion
XmlNode[] NodeArray = new XmlNode[] { Color, Style, SupNo, Class, Largness, Product, ColorAndStyle, PandL };
this.AppendXmlNode(BootNode, NodeArray);
//公共节点
XmlNode Explain = xdoc.CreateElement("Explain");
Explain.InnerText = "说明";
this.AppendSameXmlNode(NodeArray,Explain);
xdoc.AppendChild(BootNode);
xdoc.Save(XMLFILEPATH);
}
/// <summary>
/// 创建属性
/// </summary>
private XmlAttribute CreateXmlAttribute(XmlDocument xdoc,string strID, string strValue)
{
XmlAttribute xab = xdoc.CreateAttribute(strID);
xab.Value = strValue;
return xab;
}
/// <summary>
/// 加入属性
/// </summary>
private void AppendXmlAttribute(XmlNode xelt, XmlAttribute[] xab)
{
foreach (XmlAttribute xabt in xab)
{
xelt.Attributes.Append(xabt);
}
}
/// <summary>
/// 添加子节点
/// </summary>
private void AppendXmlNode(XmlNode parentNode,XmlNode[] childNodes)
{
foreach (XmlNode childNode in childNodes)
{
parentNode.AppendChild(childNode);
}
}
/// <summary>
/// 添加相同的节点
/// </summary>
private void AppendSameXmlNode(XmlNode[] parentNodes, XmlNode beAppendedNode)
{
foreach (XmlNode parentNode in parentNodes)
{
parentNode.AppendChild(beAppendedNode);
}
}
private void AppendSameXmlNode(XmlNode parentNode, XmlNode beAppendedNode)
{
parentNode.AppendChild(beAppendedNode);
}