- XML绑定TreeView
-
- private void XmlOperation_Load(object sender, EventArgs e)
- {
- path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml";
- xml.Load(path);
- bindTvXml();
- }
-
-
-
-
-
- private void bindTvXml()
- {
- for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
- {
- XmlNode Xnode = xml.DocumentElement.ChildNodes[i];
- TreeNode node = new TreeNode();
- node.Text = Xnode.Attributes["name"].Value;
- node.Tag = Xnode;
- bindChildNode(node, Xnode);
- TvXml.Nodes.Add(node);
- TvXml.HideSelection = false;
- }
- }
-
-
-
-
-
-
- private void bindChildNode(TreeNode node, XmlNode xml)
- {
- for (int i = 0; i < xml.ChildNodes.Count; i++)
- {
- TreeNode Childnode = new TreeNode();
- XmlNode ChildXml = xml.ChildNodes[i];
- Childnode.Text = ChildXml.Value;
- Childnode.Name = "1";
- Childnode.Tag = xml.ChildNodes[i];
- if (ChildXml.HasChildNodes)
- {
- if (ChildXml.ChildNodes[0].NodeType == XmlNodeType.Text)
- Childnode.Text = ChildXml.ChildNodes[0].InnerText;
- else
- bindChildNode(Childnode, ChildXml);
- }
- node.Nodes.Add(Childnode);
- }
-
- }
-
-
-
-
- public class ManageXML
- {
-
-
-
-
-
- public static string GetXMLPath(string strXMlFileName)
- {
- string m_strFullPath = "";
- Assembly Asm = Assembly.GetExecutingAssembly();
-
-
- m_strFullPath = AppDomain.CurrentDomain.BaseDirectory + "XMLLibrary\\" + strXMlFileName;
- return m_strFullPath;
- }
-
-
-
-
-
- public static DataSet GetAllDataFromXML(string strFilePath)
- {
- DataSet ds = new DataSet();
- FileInfo fileInfo = new FileInfo(strFilePath);
- if (fileInfo.Exists)
- {
- try
- {
- ds.ReadXml(strFilePath);
- }
- catch { }
- }
- else
- {
- ds = null;
- }
- if (ds != null)
- {
- if (ds.Tables[0].Rows.Count < 1)
- ds = null;
- }
- return ds;
- }
-
-
-
-
-
-
-
-
- public static Hashtable GetNodeList(string strFileName, string nodeDir)
- {
- Hashtable strNodeList = new Hashtable();
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
-
-
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
-
-
- foreach (XmlNode xn in nodeList)
- {
- XmlElement xe = (XmlElement)xn;
- strNodeList.Add(xe.GetAttribute("id").ToString(), xe.InnerText.Trim());
- }
-
-
- }
- catch (Exception)
- {
-
-
- throw;
- }
- return strNodeList;
- }
-
-
-
-
-
-
-
-
-
-
- public static string GetNodeValue(string strFileName, string nodeName, string nodeDir)
- {
- string value = null;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
-
-
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
-
-
- foreach (XmlNode xn in nodeList)
- {
- XmlElement xe = (XmlElement)xn;
-
-
- if (xe.Name == nodeName)
- {
- value = xe.InnerText.Trim();
-
-
- break;
- }
- }
- }
- catch (Exception exp)
- {
- throw exp;
- }
-
-
- return value;
- }
-
-
-
-
-
-
-
-
-
-
- public static string GetNodeValue(string strFileName, string nodeName, string nodeDir, string attribute)
- {
- string value = null;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
-
-
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
-
-
- foreach (XmlNode xn in nodeList)
- {
- XmlElement xe = (XmlElement)xn;
-
-
- if (xe.Name == nodeName)
- {
-
- value = (xe).Attributes[attribute].Value;
- break;
- }
- }
- }
- catch (Exception exp)
- {
- throw exp;
- }
-
-
- return value;
- }
-
-
-
-
-
-
-
-
-
-
- public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir)
- {
- bool isSucceed = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
-
-
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
-
-
- foreach (XmlNode xn in nodeList)
- {
- XmlElement xe = (XmlElement)xn;
-
-
- if (xe.Name == nodeName)
- {
- xe.InnerText = value;
-
-
- isSucceed = true;
- break;
- }
- }
-
-
- xmlDoc.Save(strFileName);
- }
- catch (Exception exp)
- {
- throw exp;
- }
-
-
- return isSucceed;
- }
-
-
-
-
-
-
-
-
-
-
- public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir, string attribute, string attributeValue)
- {
- bool isSucceed = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
-
-
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
-
-
- foreach (XmlNode xn in nodeList)
- {
- XmlElement xe = (XmlElement)xn;
-
-
- if (xe.Name == nodeName)
- {
- xe.InnerText = value;
- (xe).Attributes[attribute].Value = attributeValue;
- isSucceed = true;
- break;
- }
- }
-
-
- xmlDoc.Save(strFileName);
- }
- catch (Exception exp)
- {
- throw exp;
- }
-
-
- return isSucceed;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool UpdateNoteValue(string strFileName, Hashtable hstable, string nodeDir)
- {
- bool isSucceed = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
-
-
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach (DictionaryEntry item in hstable)
- {
- foreach (XmlNode xn in nodeList)
- {
- XmlElement xe = (XmlElement)xn;
-
-
- if (xe.Name == item.Key.ToString())
- {
- xe.InnerText = item.Value.ToString();
-
-
- isSucceed = true;
- break;
- }
- }
- }
-
-
- xmlDoc.Save(strFileName);
- }
- catch (Exception exp)
- {
- throw exp;
- }
-
-
- return isSucceed;
- }
-
- }
-
-
-
-
- public void RemoveAllAttribute(string xmlPathNode)
- {
- XmlNodeList xnl = objXmlDoc.SelectNodes(xmlPathNode);
- foreach (XmlNode xn in xnl)
- {
- xn.Attributes.RemoveAll();
- }
- }
- public void DeleteNode(string Node)
- {
-
- try
- {
- string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
- objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
- }
- catch
- {
-
- return;
- }
- }
- public void InsertNodeWithChild(string mainNode, string ChildNode, string Element, string Content)
- {
-
- XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
- objRootNode.AppendChild(objChildNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objElement.InnerText = Content;
- objChildNode.AppendChild(objElement);
- }
-
-
-
-
-
-
-
-
- public void InsertNode(string mainNode, string Element, string Attrib, string AttribContent, string Content)
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objElement.SetAttribute(Attrib, AttribContent);
- objElement.InnerText = Content;
- objNode.AppendChild(objElement);
- }
-
-
-
-
-
-
-
- public void InsertNode(string mainNode, string Element, string Attrib, string AttribContent)
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objElement.SetAttribute(Attrib, AttribContent);
- objNode.AppendChild(objElement);
- }
-
-
-
-
-
- public void InsertNode(string mainNode, string Element)
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objNode.AppendChild(objElement);
- }
-
-
-
- public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
- {
- try
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(elementName);
- for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
- {
- objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
- }
- objElement.InnerText = elementContent;
- objNode.AppendChild(objElement);
- }
- catch
- {
- throw;
-
- }
- }
-
-
-
- public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent)
- {
- try
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(elementName);
- for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
- {
- objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
- }
-
- objNode.AppendChild(objElement);
- }
- catch
- {
- throw;
-
- }
- }
-
-
-
-
-
-
-
-
- public void AddChildNode(string parentNodePath, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
- {
- try
- {
- XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
- for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
- {
- objChildElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
- }
- objChildElement.InnerText = elementContent;
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
-
-
-
-
-
-
- public void AddChildNodeCData(string parentNodePath, string elementName, string elementContent)
- {
- try
- {
- XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
-
- XmlCDataSection xcds = objXmlDoc.CreateCDataSection(elementContent);
- objChildElement.AppendChild(xcds);
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
-
-
-
-
-
-
-
-
- public void AddChildNode(string parentNodePath, string elementName, string elementContent)
- {
- try
- {
- XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
- objChildElement.InnerText = elementContent;
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
-
-
-
-
-
- public bool FindNode(string NodePath)
- {
- try
- {
- if (objXmlDoc.SelectSingleNode(NodePath) != null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
posted @
2013-08-27 09:19
lvyafei
阅读(
179)
评论()
编辑
收藏
举报