在treeView里面,程序启动以后加载本地xml数据,我想在点击数据以后,在添加新的数据到子节点下。首先加载的xml文件代码:<?xml version="1.0" encoding="utf-8"?>
<movies name="连载更新列表">
<m_name id="1" name="2009传说中的故乡" filename="h_912.xml"/>
<m_name id="2" name="我们结婚了(第2季)" filename="h_911.xml"/>
<m_name id="3" name="乌龙派出所" filename="r_908.xml"/>
</movies>当双击其中一个名称时(如:2009传说中的故乡),程序就自动将h_912.xml的内容添加到它的下面。其中h_912.xml放在服务器上,可以直接加载,也可以下回本地才加载也行。h_912.xml内容如下:<?xml version="1.0" encoding="utf-8"?>
<m_name id="912" name="2009传说中的故乡">
<several name="第1集" url="/play/youku.html?id=27955204"/>
<several name="第2集" url="/play/youku.html?id=28020104"/>
<several name="第3集" url="/play/youku.html?id=28341494"/>
<several name="第4集" url="/play/youku.html?id=28425984"/>
<several name="第5集" url="/play/youku.html?id=28739961"/>
<several name="第6集" url="/play/youku.html?id=28772845"/>
</m_name >要实现的目标是:启动程序自动加载本地xml文件,当用户点击某个电影名字时,获取它的xml文件名,去服务器上查找这个文件并把内容添加到这个电影下面,然后用户点击相应的集数才开始播放。本来打算将数据写在同一个xml下,但是这样导致文件体积太大了所以才想到这样的办法,但不知道怎么做。这是之前的代码,需要怎么修改 //显示根节点
private void bind(string path, string FileName)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path + FileName.Trim());
XmlElement root = xmlDoc.DocumentElement;
TreeNode trRoot = new TreeNode(root.Attributes["name"].Value);
this.treeView1.Nodes.Add(trRoot);
for (int i = 0; i < root.ChildNodes.Count; i++)
{
TreeNode trNode = new TreeNode(root.ChildNodes[i].Attributes["name"].Value);
XmlNode xmlNode = root.ChildNodes[i];
trRoot.Nodes.Add(trNode);
setChild(trNode, xmlNode);
}
}
//显示子节点
private void setChild(TreeNode node, XmlNode xmlNode)
{
int num = 0;
foreach (XmlNode nodexml in xmlNode.ChildNodes)
{
TreeNode nd = new TreeNode(nodexml.Attributes["name"].Value);
nd.Tag = nodexml.Attributes["url"].Value;
TreeNode nodes = new TreeNode();
XmlNode xmlNd = xmlNode.ChildNodes[num];
node.Nodes.Add(nd);
setChild(nd, xmlNd);
num++;
}
}
<movies name="连载更新列表">
<m_name id="1" name="2009传说中的故乡" filename="h_912.xml"/>
<m_name id="2" name="我们结婚了(第2季)" filename="h_911.xml"/>
<m_name id="3" name="乌龙派出所" filename="r_908.xml"/>
</movies>当双击其中一个名称时(如:2009传说中的故乡),程序就自动将h_912.xml的内容添加到它的下面。其中h_912.xml放在服务器上,可以直接加载,也可以下回本地才加载也行。h_912.xml内容如下:<?xml version="1.0" encoding="utf-8"?>
<m_name id="912" name="2009传说中的故乡">
<several name="第1集" url="/play/youku.html?id=27955204"/>
<several name="第2集" url="/play/youku.html?id=28020104"/>
<several name="第3集" url="/play/youku.html?id=28341494"/>
<several name="第4集" url="/play/youku.html?id=28425984"/>
<several name="第5集" url="/play/youku.html?id=28739961"/>
<several name="第6集" url="/play/youku.html?id=28772845"/>
</m_name >要实现的目标是:启动程序自动加载本地xml文件,当用户点击某个电影名字时,获取它的xml文件名,去服务器上查找这个文件并把内容添加到这个电影下面,然后用户点击相应的集数才开始播放。本来打算将数据写在同一个xml下,但是这样导致文件体积太大了所以才想到这样的办法,但不知道怎么做。这是之前的代码,需要怎么修改 //显示根节点
private void bind(string path, string FileName)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path + FileName.Trim());
XmlElement root = xmlDoc.DocumentElement;
TreeNode trRoot = new TreeNode(root.Attributes["name"].Value);
this.treeView1.Nodes.Add(trRoot);
for (int i = 0; i < root.ChildNodes.Count; i++)
{
TreeNode trNode = new TreeNode(root.ChildNodes[i].Attributes["name"].Value);
XmlNode xmlNode = root.ChildNodes[i];
trRoot.Nodes.Add(trNode);
setChild(trNode, xmlNode);
}
}
//显示子节点
private void setChild(TreeNode node, XmlNode xmlNode)
{
int num = 0;
foreach (XmlNode nodexml in xmlNode.ChildNodes)
{
TreeNode nd = new TreeNode(nodexml.Attributes["name"].Value);
nd.Tag = nodexml.Attributes["url"].Value;
TreeNode nodes = new TreeNode();
XmlNode xmlNd = xmlNode.ChildNodes[num];
node.Nodes.Add(nd);
setChild(nd, xmlNd);
num++;
}
}
你也可以考虑改成异步下载和加载节点的方式. treeView1.AfterSelect +=(s,e)=>
{
//判断未曾加载,且是首层节点,且Tag!=null
if (e.Node.Nodes.Count == 0 && e.Node.Level == 0 && e.Node.Tag != null)
{
string url = e.Node.Tag.ToString();
WebClient client = new WebClient();
string localPath = @"C:\\data.xml";
client.DownloadFile(url, localPath);
XmlDocument doc = new XmlDocument();
doc.Load(localPath);
setChild(e.Node, doc.DocumentElement);
}
};
treeview分布加载数据,点击节点再加载
public void Insert(string path, string node, string element, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
{
xe.InnerText = value;
}
else
{
xe.SetAttribute(attribute, value);
}
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}