前端之家收集整理的这篇文章主要介绍了
xml操作类 c#,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- XML绑定TreeView
- privatevoidXmlOperation_Load(objectsender,EventArgse)
- {
- path=AppDomain.CurrentDomain.BaseDirectory+@"NameList.xml";
- xml.Load(path);
- bindTvXml();
- }
-
- privatevoidbindTvXml()
- {
- for(inti=0;i<xml.DocumentElement.ChildNodes.Count;i++)
- {
- XmlNodeXnode=xml.DocumentElement.ChildNodes[i];
- TreeNodenode=newTreeNode();
- node.Text=Xnode.Attributes["name"].Value;
- node.Tag=Xnode;
- bindChildNode(node,Xnode);
- TvXml.Nodes.Add(node);
- TvXml.HideSelection=false;
- }
- }
-
- privatevoidbindChildNode(TreeNodenode,XmlNodexml)
- {
- for(inti=0;i<xml.ChildNodes.Count;i++)
- {
- TreeNodeChildnode=newTreeNode();
- XmlNodeChildXml=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);
- }
- }
- publicclassManageXML
- {
-
- publicstaticstringGetXMLPath(stringstrXMlFileName)
- {
- stringm_strFullPath="";
- AssemblyAsm=Assembly.GetExecutingAssembly();
-
- m_strFullPath=AppDomain.CurrentDomain.BaseDirectory+"XMLLibrary\\"+strXMlFileName;
- returnm_strFullPath;
- }
-
- publicstaticDataSetGetAllDataFromXML(stringstrFilePath)
- {
- DataSetds=newDataSet();
- FileInfofileInfo=newFileInfo(strFilePath);
- if(fileInfo.Exists)
- {
- try
- {
- ds.ReadXml(strFilePath);
- }
- catch{}
- }
- else
- {
- ds=null;
- }
- if(ds!=null)
- {
- if(ds.Tables[0].Rows.Count<1)
- ds=null;
- }
- returnds;
- }
-
- publicstaticHashtableGetNodeList(stringstrFileName,stringnodeDir)
- {
- HashtablestrNodeList=newHashtable();
- try
- {
- XmlDocumentxmlDoc=newXmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach(XmlNodexninnodeList)
- {
- XmlElementxe=(XmlElement)xn;
- strNodeList.Add(xe.GetAttribute("id").ToString(),xe.InnerText.Trim());
- }
- }
- catch(Exception)
- {
- throw;
- }
- returnstrNodeList;
- }
-
- publicstaticstringGetNodeValue(stringstrFileName,stringnodeName,stringnodeDir)
- {
- stringvalue=null;
- try
- {
- XmlDocumentxmlDoc=newXmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach(XmlNodexninnodeList)
- {
- XmlElementxe=(XmlElement)xn;
- if(xe.Name==nodeName)
- {
- value=xe.InnerText.Trim();
- break;
- }
- }
- }
- catch(Exceptionexp)
- {
- throwexp;
- }
- returnvalue;
- }
-
- publicstaticstringGetNodeValue(stringstrFileName,stringnodeDir,stringattribute)
- {
- stringvalue=null;
- try
- {
- XmlDocumentxmlDoc=newXmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach(XmlNodexninnodeList)
- {
- XmlElementxe=(XmlElement)xn;
- if(xe.Name==nodeName)
- {
- value=(xe).Attributes[attribute].Value;
- break;
- }
- }
- }
- catch(Exceptionexp)
- {
- throwexp;
- }
- returnvalue;
- }
- publicstaticboolUpdateNoteValue(stringstrFileName,stringvalue,stringnodeDir)
- {
- boolisSucceed=false;
- try
- {
- XmlDocumentxmlDoc=newXmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach(XmlNodexninnodeList)
- {
- XmlElementxe=(XmlElement)xn;
- if(xe.Name==nodeName)
- {
- xe.InnerText=value;
- isSucceed=true;
- break;
- }
- }
- xmlDoc.Save(strFileName);
- }
- catch(Exceptionexp)
- {
- throwexp;
- }
- returnisSucceed;
- }
- publicstaticboolUpdateNoteValue(stringstrFileName,stringattribute,stringattributeValue)
- {
- boolisSucceed=false;
- try
- {
- XmlDocumentxmlDoc=newXmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach(XmlNodexninnodeList)
- {
- XmlElementxe=(XmlElement)xn;
- if(xe.Name==nodeName)
- {
- xe.InnerText=value;
- (xe).Attributes[attribute].Value=attributeValue;
- isSucceed=true;
- break;
- }
- }
- xmlDoc.Save(strFileName);
- }
- catch(Exceptionexp)
- {
- throwexp;
- }
- returnisSucceed;
- }
-
- publicstaticboolUpdateNoteValue(stringstrFileName,Hashtablehstable,stringnodeDir)
- {
- boolisSucceed=false;
- try
- {
- XmlDocumentxmlDoc=newXmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;
- foreach(DictionaryEntryiteminhstable)
- {
- foreach(XmlNodexninnodeList)
- {
- XmlElementxe=(XmlElement)xn;
- if(xe.Name==item.Key.ToString())
- {
- xe.InnerText=item.Value.ToString();
- isSucceed=true;
- break;
- }
- }
- }
- xmlDoc.Save(strFileName);
- }
- catch(Exceptionexp)
- {
- throwexp;
- }
- returnisSucceed;
- }
- }
XML绑定TreeView
private void XmlOperation_Load(object sender,EventArgs e)
{
path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml";
xml.Load(path);//加载xml文件
bindTvXml();
}
/// <summary>
/// 绑定TreeView
/// </summary>
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;
}
}
/// <summary>
/// 递归绑定子节点
/// </summary>
/// <param name="node"></param>
/// <param name="xml"></param>
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
{
/// <summary>
/// 返回用户操作的sql语句
/// </summary>
/// <param name="sqlNodeName">XML的操作节点</param>
/// <returns>操作的sql语句</returns>
public static string GetXMLPath(string strXMlFileName)
{
string m_strFullPath = "";
Assembly Asm = Assembly.GetExecutingAssembly();
//获取文件的路径
//m_strFullPath = Asm.Location.Substring(0,(Asm.Location.LastIndexOf("\\") + 1)) + "XMLsql.xml";
m_strFullPath = AppDomain.CurrentDomain.BaseDirectory + "XMLLibrary\\" + strXMlFileName;
return m_strFullPath;
}
/// <summary>
/// 获取XML数据库中的数据的方法
/// </summary>
/// <param name="strFilePath">传入文件路径</param>
/// <returns>返回一个数据集</returns>
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;
}
/// <summary>
/// 获取指定目录下所有子节点的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeDir">节点目录</param>
/// <returns></returns>
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;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
strNodeList.Add(xe.GetAttribute("id").ToString(),xe.InnerText.Trim());
}
}
catch (Exception)
{
throw;
}
return strNodeList;
}
/// <summary>
/// 获取指定节点的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
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;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
value = xe.InnerText.Trim();
break;
}
}
}
catch (Exception exp)
{
throw exp;
}
return value;
}
/// <summary>
/// 获取指定节点下面对应属性的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <param name="attribute">节点对应的属性名称</param>
/// <returns></returns>
public static string GetNodeValue(string strFileName,string nodeDir,string attribute)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
//value = xe.InnerText.Trim();
value = (xe).Attributes[attribute].Value;
break;
}
}
}
catch (Exception exp)
{
throw exp;
}
return value;
}
/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateNoteValue(string strFileName,string value,string nodeDir)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
xe.InnerText = value;
isSucceed = true;
break;
}
}
xmlDoc.Save(strFileName);
}
catch (Exception exp)
{
throw exp;
}
return isSucceed;
}
/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateNoteValue(string strFileName,string attribute,string attributeValue)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
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;
}
/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
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;//获取bookstore节点的所有子节点
foreach (DictionaryEntry item in hstable)
{
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == item.Key.ToString())
{
xe.InnerText = item.Value.ToString();
isSucceed = true;
break;
}
}
}
xmlDoc.Save(strFileName);
}
catch (Exception exp)
{
throw exp;
}
return isSucceed;
}
}
-
-
- publicvoidRemoveAllAttribute(stringxmlPathNode)
- {
- XmlNodeListxnl=objXmlDoc.SelectNodes(xmlPathNode);
- foreach(XmlNodexninxnl)
- {
- xn.Attributes.RemoveAll();
- }
- }
- publicvoidDeleteNode(stringNode)
- {
- try
- {
- stringmainNode=Node.Substring(0,Node.LastIndexOf("/"));
- objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
- }
- catch
- {
-
- return;
- }
- }
- publicvoidInsertNodeWithChild(stringmainNode,stringChildNode,stringElement,stringContent)
- {
-
- XmlNodeobjRootNode=objXmlDoc.SelectSingleNode(mainNode);
- XmlElementobjChildNode=objXmlDoc.CreateElement(ChildNode);
- objRootNode.AppendChild(objChildNode);
- XmlElementobjElement=objXmlDoc.CreateElement(Element);
- objElement.InnerText=Content;
- objChildNode.AppendChild(objElement);
- }
-
-
- publicvoidInsertNode(stringmainNode,stringAttrib,stringAttribContent,stringContent)
- {
- XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
- XmlElementobjElement=objXmlDoc.CreateElement(Element);
- objElement.SetAttribute(Attrib,AttribContent);
- objElement.InnerText=Content;
- objNode.AppendChild(objElement);
- }
-
- publicvoidInsertNode(stringmainNode,stringAttribContent)
- {
- XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
- XmlElementobjElement=objXmlDoc.CreateElement(Element);
- objElement.SetAttribute(Attrib,AttribContent);
- objNode.AppendChild(objElement);
- }
-
- publicvoidInsertNode(stringmainNode,stringElement)
- {
- XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
- XmlElementobjElement=objXmlDoc.CreateElement(Element);
- objNode.AppendChild(objElement);
- }
- publicvoidInsertNode(stringmainNode,stringelementName,string[]arrAttributeName,string[]arrAttributeContent,stringelementContent)
- {
- try
- {
- XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
- XmlElementobjElement=objXmlDoc.CreateElement(elementName);
- for(inti=0;i<=arrAttributeName.GetUpperBound(0);i++)
- {
- objElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
- }
- objElement.InnerText=elementContent;
- objNode.AppendChild(objElement);
- }
- catch
- {
- throw;
- }
- }
-
- publicvoidInsertNode(stringmainNode,string[]arrAttributeContent)
- {
- try
- {
- XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
- XmlElementobjElement=objXmlDoc.CreateElement(elementName);
- for(inti=0;i<=arrAttributeName.GetUpperBound(0);i++)
- {
- objElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
- }
- objNode.AppendChild(objElement);
- }
- catch
- {
- throw;
- }
- }
-
- publicvoidAddChildNode(stringparentNodePath,stringelementContent)
- {
- try
- {
- XmlNodeparentNode=objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElementobjChildElement=objXmlDoc.CreateElement(elementName);
- for(inti=0;i<=arrAttributeName.GetUpperBound(0);i++)
- {
- objChildElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
- }
- objChildElement.InnerText=elementContent;
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
- publicvoidAddChildNodeCData(stringparentNodePath,stringelementContent)
- {
- try
- {
- XmlNodeparentNode=objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElementobjChildElement=objXmlDoc.CreateElement(elementName);
- XmlCDataSectionxcds=objXmlDoc.CreateCDataSection(elementContent);
- objChildElement.AppendChild(xcds);
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
-
- publicvoidAddChildNode(stringparentNodePath,stringelementContent)
- {
- try
- {
- XmlNodeparentNode=objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElementobjChildElement=objXmlDoc.CreateElement(elementName);
- objChildElement.InnerText=elementContent;
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
-
- publicboolFindNode(stringNodePath)
- {
- try
- {
- if(objXmlDoc.SelectSingleNode(NodePath)!=null)
- {
- returntrue;
- }
- else
- {
- returnfalse;
- }
- }
- catch
- {
- returnfalse;
- }
- }