原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/2018/1011/64.html
		1.创建XML文档:
	
		  /// 创建XML文档
	
		  /// 根节点名称
	
		  /// 根节点的一个属性值
	
		  public static XmlDocument CreateXmlDocument(string name, string type)
	
		  {
	
		   XmlDocument doc;
	
		   try
	
		   {
	
		    doc = new XmlDocument();
	
		    doc.LoadXml("<" + name + "/>");
	
		    var rootEle = doc.DocumentElement;
	
		    rootEle?.SetAttribute("type", type);
	
		   }
	
		   catch (Exception er)
	
		   {
	
		    throw new Exception(er.ToString());
	
		   }
	
		   return doc;
	
		  }
	
		2.读取XML文档中的数据:
	
		  /// 读取数据
	
		  /// 路径
	
		  /// 节点
	
		  /// 属性名,非空时返回该属性值,否则返回串联值
	
		  /// string
	
		  public static string Read(string path, string node, string attribute)
	
		  {
	
		   var value = "";
	
		   try
	
		   {
	
		    var doc = new XmlDocument();
	
		    doc.Load(path);
	
		    var xn = doc.SelectSingleNode(node);
	
		    if (xn != null && xn.Attributes != null)
	
		     value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
	
		   }
	
		   catch (Exception er)
	
		   {
	
		    throw new Exception(er.ToString());
	
		   }
	
		   return value;
	
		  }
	
		3.对XML文档插入数据:
	
		  /// 插入数据
	
		  /// 路径
	
		  /// 节点
	
		  /// 元素名,非空时插入新元素,否则在该元素中插入属性
	
		  /// 属性名,非空时插入该元素属性值,否则插入元素值
	
		  /// 值
	
		  public static void Insert(string path, string node, string element, string attribute, string value)
	
		  {
	
		   try
	
		   {
	
		    var doc = new XmlDocument();
	
		    doc.Load(path);
	
		    var xn = doc.SelectSingleNode(node);
	
		    if (element.Equals(""))
	
		    {
	
		     if (!attribute.Equals(""))
	
		     {
	
		      var xe = (XmlElement)xn;
	
		      xe?.SetAttribute(attribute, value);
	
		      //xe?.SetAttribute(attribute, value);
	
		     }
	
		    }
	
		    else
	
		    {
	
		     var xe = doc.createElement_x(element);
	
		     if (attribute.Equals(""))
	
		      xe.InnerText = value;
	
		     else
	
		      xe.SetAttribute(attribute, value);
	
		     xn?.A(xe);
	
		    }
	
		    doc.Save(path);
	
		   }
	
		   catch (Exception er)
	
		   {
	
		    throw new Exception(er.ToString());
	
		   }
	
		  }
	
		4.修改XML文档中的数据:
	
		  /// 修改数据
	
		  /// 路径
	
		  /// 节点
	
		  /// 属性名,非空时修改该节点属性值,否则修改节点值
	
		  /// 值
	
		  public static void Update(string path, string node, string attribute, string value)
	
		  {
	
		   try
	
		   {
	
		    var doc = new XmlDocument();
	
		    doc.Load(path);
	
		    var xn = doc.SelectSingleNode(node);
	
		    var xe = (XmlElement)xn;
	
		    if (attribute.Equals(""))
	
		    {
	
		     if (xe != null) xe.InnerText = value;
	
		    }
	
		    else
	
		    {
	
		     xe?.SetAttribute(attribute, value);
	
		    }
	
		    doc.Save(path);
	
		   }
	
		   catch (Exception er)
	
		   {
	
		    throw new Exception(er.ToString());
	
		   }
	
		  }
	
		5.删除XML文档中数据:
	
		  /// 删除数据
	
		  /// 路径
	
		  /// 节点
	
		  /// 属性名,非空时删除该节点属性值,否则删除节点值
	
		  public static void Delete(string path, string node, string attribute)
	
		  {
	
		   try
	
		   {
	
		    var doc = new XmlDocument();
	
		    doc.Load(path);
	
		    var xn = doc.SelectSingleNode(node);
	
		    var xe = (XmlElement)xn;
	
		    if (attribute.Equals(""))
	
		    {
	
		     xn?.ParentNode?.RemoveChild(xn);
	
		    }
	
		    else
	
		    {
	
		     xe?.RemoveAttribute(attribute);
	
		    }
	
		    doc.Save(path);
	
		   }
	
		   catch (Exception er)
	
		   {
	
		    throw new Exception(er.ToString());
	
		   }
	
		  }
	
		6.读取XML文档中指定节点数据:
	
		  /// 获得xml文件中指定节点的节点数据
	
		  ///
	
		  ///
	
		  public static string GetNodeInfoByNodeName(string path, string nodeName)
	
		  {
	
		   var xmlString = string.Empty;
	
		   try
	
		   {
	
		    var xml = new XmlDocument();
	
		    xml.Load(path);
	
		    var root = xml.DocumentElement;
	
		    if (root == null) return xmlString;
	
		    var node = root.SelectSingleNode("//" + nodeName);
	
		    if (node != null)
	
		    {
	
		     xmlString = node.InnerText;
	
		    }
	
		   }
	
		   catch (Exception er)
	
		   {
	
		    throw new Exception(er.ToString());
	
		   }
	
		   return xmlString;
	
		  }
	
		7.获取XML指定节点的属性:
	
		  /// 功能:读取指定节点的指定属性值  
	
		  ///
	
		  /// 节点名称 
	
		  /// 此节点的属性 
	
		  public string GetXmlNodeAttributeValue(string path, string strNode, string strAttribute)
	
		  {
	
		   var strReturn = "";
	
		   try
	
		   {
	
		    var xml = new XmlDocument();
	
		    xml.Load(path);
	
		    //根据指定路径获取节点 
	
		    var xmlNode = xml.SelectSingleNode(strNode);
	
		    if (xmlNode != null)
	
		    {
	
		     //获取节点的属性,并循环取出需要的属性值 
	
		     var xmlAttr = xmlNode.Attributes;
	
		     if (xmlAttr == null) return strReturn;
	
		     for (var i = 0; i < xmlAttr.Count; i++)
	
		     {
	
		      if (xmlAttr.Item(i).Name != strAttribute) continue;
	
		      strReturn = xmlAttr.Item(i).Value;
	
		      break;
	
		     }
	
		    }
	
		   }
	
		   catch (XmlException xmle)
	
		   {
	
		    throw new Exception(xmle.Message);
	
		   }
	
		   return strReturn;
	
		  }
	
		8.设置XML文档中指定节点的属性:
	
		  /// 功能:设置节点的属性值  
	
		  ///
	
		  /// 节点名称 
	
		  /// 属性名称 
	
		  /// 属性值 
	
		  public void SetXmlNodeAttributeValue(string path, string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)
	
		  {
	
		   try
	
		   {
	
		    var xml = new XmlDocument();
	
		    xml.Load(path);
	
		    //可以批量为符合条件的节点的属性付值 
	
		    var xmlNode = xml.SelectNodes(xmlNodePath);
	
		    if (xmlNode == null) return;
	
		    foreach (var xmlAttr in from XmlNode xn in xmlNode select xn.Attributes)
	
		    {
	
		     if (xmlAttr == null) return;
	
		     for (var i = 0; i < xmlAttr.Count; i++)
	
		     {
	
		      if (xmlAttr.Item(i).Name != xmlNodeAttribute) continue;
	
		      xmlAttr.Item(i).Value = xmlNodeAttributeValue;
	
		      break;
	
		     }
	
		    }
	
		   }
	
		   catch (XmlException xmle)
	
		   {
	
		    throw new Exception(xmle.Message);
	
		   }
	
		  }
	
		9.读取XML文档指定节点的值:
	
		  /// 读取XML资源中的指定节点内容
	
		  /// XML资源
	
		  /// XML资源类型:文件,字符串
	
		  /// 节点名称
	
		  public static object GetNodeValue(string source, XmlType xmlType, string nodeName)
	
		  {
	
		   var xd = new XmlDocument();
	
		   if (xmlType == XmlType.File)
	
		   {
	
		    xd.Load(source);
	
		   }
	
		   else
	
		   {
	
		    xd.LoadXml(source);
	
		   }
	
		   var xe = xd.DocumentElement;
	
		   XmlNode xn = null;
	
		   if (xe != null)
	
		   {
	
		     xn= xe.SelectSingleNode("//" + nodeName);
	
		   }
	
		   return xn.InnerText;
	
		  }
	
		10.更新XML文档指定节点的内容:
	
		  /// 更新XML文件中的指定节点内容
	
		  /// 文件路径
	
		  /// 节点名称
	
		  /// 更新内容
	
		  public static bool UpdateNode(string filePath, string nodeName, string nodeValue)
	
		  {   
	
		   try
	
		   {
	
		    bool flag;
	
		    var xd = new XmlDocument();
	
		    xd.Load(filePath);
	
		    var xe = xd.DocumentElement;
	
		    if (xe == null) return false;
	
		    var xn = xe.SelectSingleNode("//" + nodeName);
	
		    if (xn != null)
	
		    {
	
		     xn.InnerText = nodeValue;
	
		     flag = true;
	
		    }
	
		    else
	
		    {
	
		     flag = false;
	
		    }
	
		    return flag;
	
		   }
	
		   catch (Exception ex)
	
		   {
	
		    throw new Exception(ex.Message);
	
		   }
	
		  }
	
		复制代码
	
		11.将对象转化为XML文件,并存入指定目录:
	
		复制代码
	
		  /// 将对象转化为xml,并写入指定路径的xml文件中
	
		  /// C#对象名
	
		  /// 对象实例
	
		  /// 路径
	
		  /// 标号
	
		  /// 结束符号(C:\xmltest\201111send.xml,其中path=C:\xmltest,jjdbh=201111,ends=send)
	
		  public static string WriteXml(T item, string path, string jjdbh, string ends)
	
		  {
	
		   if (string.IsNullOrEmpty(ends))
	
		   {
	
		    //默认为发送
	
		    ends = "send";
	
		   }
	
		   //控制写入文件的次数
	
		   var i = 0;
	
		   //获取当前对象的类型,也可以使用反射typeof(对象名)
	
		   var serializer = new XmlSerializer(item.GetType());
	
		   //xml的路径组合
	
		   object[] obj = { path, "\\", jjdbh, ends, ".xml" };
	
		   var xmlPath = string.Concat(obj);
	
		   while (true)
	
		   {
	
		    try
	
		    {
	
		     //用filestream方式创建文件不会出现“文件正在占用中,用File.create”则不行
	
		     var fs = System.IO.File.Create(xmlPath);
	
		     fs.Close();
	
		     TextWriter writer = new StreamWriter(xmlPath, false, Encoding.UTF8);
	
		     var xml = new XmlSerializerNamespaces();
	
		     xml.Add(string.Empty, string.Empty);
	
		     serializer.Serialize(writer, item, xml);
	
		     writer.Flush();
	
		     writer.Close();
	
		     break;
	
		    }
	
		    catch (Exception)
	
		    {
	
		     if (i < 5)
	
		     {
	
		      i++;
	
		      continue;
	
		     }
	
		     break;
	
		    }
	
		   }
	
		   return SerializeToXmlStr(item, true);
	
		  }
	
		12.向一个已经存在的父节点中插入一个子节点:
	
		  /// 向一个已经存在的父节点中插入一个子节点 
	
		  ///
	
		  /// 父节点
	
		  /// 子节点名称 
	
		  public void AddChildNode(string path, string parentNodePath, string childnodename)
	
		  {
	
		   try
	
		   {
	
		    var xml = new XmlDocument();
	
		    xml.Load(path);
	
		    var parentXmlNode = xml.SelectSingleNode(parentNodePath);
	
		    XmlNode childXmlNode = xml.createElement_x(childnodename);
	
		    if ((parentXmlNode) != null)
	
		    {
	
		     //如果此节点存在 
	
		     parentXmlNode.A(childXmlNode);
	
		    }
	
		    else
	
		    {
	
		     //如果不存在就放父节点添加 
	
		     GetXmlRoot(path).A(childXmlNode);
	
		    }
	
		   }
	
		   catch (XmlException xmle)
	
		   {
	
		    throw new Exception(xmle.Message);
	
		   }
	
		  }
上篇:上一篇:以给定值为基分割链表
下篇:下一篇:简单二叉排序树的实现



