xml文件的节点值获取和节点值设置
/// <summary> /// 保存单个点节点的值 /// </summary> /// <param name="UpNodeName">父级节点名称</param> /// <param name="NodeName">子节点名称</param> /// <param name="NodeNumber">子节点值</param> /// <returns></returns> public static bool SaveInfoXMl(string UpNodeName, string NodeName, string NodeNumber) { XmlDocument doc = new XmlDocument(); try { doc.Load("ArchivesConfig.xml");//加载XML文件,ArchivesConfig.xml是xml文件名 XmlNode xl = doc.SelectSingleNode("YY_ArchivesMangeConfig");//根节点1,YY_ArchivesMangeConfig是根节点名称 XmlNode xll = xl.SelectSingleNode(UpNodeName); XmlNode xmlnod = xll.SelectSingleNode(NodeName); ((XmlElement)xmlnod).InnerText = NodeNumber; doc.Save("ArchivesConfig.xml"); return true; } catch { return false; } } /// <summary> /// 根据父亲节点和子节点名称获取子节点的值 /// </summary> /// <param name="UpNodeName">父节点</param> /// <param name="NodeName">子节点</param> /// <returns></returns> public static string GetXmlNumber(string UpNodeName, string NodeName) { try { XmlDocument doc = new XmlDocument(); doc.Load("ArchivesConfig.xml");//加载XML文件,ArchivesConfig.xml是xml文件名 XmlNode xl = doc.SelectSingleNode("YY_ArchivesMangeConfig");//根节点1,YY_ArchivesMangeConfig是根节点名称 XmlNode xll = xl.SelectSingleNode(UpNodeName); XmlNode xmlnod = xll.SelectSingleNode(NodeName); string Number = xmlnod.InnerText; Number = Number.Replace("<", "<"); Number = Number.Replace(">", ">"); Number = Number.Replace("&", "&"); Number = Number.Replace(" '", "'"); return xmlnod.InnerText; } catch { return null; } }
使用时只需要调用上面的方法就好了,再传对应的参数