XML 操作,高级查询

前端之家收集整理的这篇文章主要介绍了XML 操作,高级查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
XML 操作
DataSet ds = new DataSet();
      string filePath = Server.MapPath("~\\XmlFile\\item\\Suppliers.xml");
      ds.ReadXml(filePath);

       rptXml.DataSource = ds.Tables["Supplier"].DefaultView;
      rptXml.DataBind();


public partial class ajax_ajaxSuppliers : System.Web.UI.Page
{
    /// <summary>
    /// 返回数据
    /// </summary>
    private string response = string.Empty;

    /// <summary>
    /// Page_Load
    /// </summary>
    /// <param name="sender">sender</param>
    /// <param name="e">e</param>
    protected void Page_Load(object sender,EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["action"]))
        {
            string action = Request["action"].ToString().Trim(); // 操作类别
            switch (action.ToLower())
            {
                // 获取省份
                case "savesuppliers":
                    SaveSuppliers();
                    break;
                default:
                    break;
            }

            Response.Write(this.response);
        }
    }

    /// <summary>
    /// 保存供应商信息
    /// </summary>
    private void SaveSuppliers()
    {
        string folderPath = Server.MapPath("~\\XmlFile\\item");
        string filePath = folderPath + "\\Suppliers.xml";
        XmlDocument doc = LoadXMLDoc(folderPath,filePath);
        FullXMLNode(ref doc,this.ConfigXmlLabel());
        doc.Save(filePath);
        response = "感谢您的反馈,我们会尽快给你答复。";
    }

    /// <summary>
    /// 加载XML文件
    /// </summary>
    /// <param name="folderPath">文件夹路径</param>
    /// <param name="filePath">文件路径</param>
    /// <returns>XmlDocument</returns>
    private XmlDocument LoadXMLDoc(string folderPath,string filePath)
    {
        XmlDocument doc = new XmlDocument();
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }

        if (!File.Exists(filePath))
        {
            // 创建XML 头
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","utf-8",null);
            doc.AppendChild(dec);

            // 创建一个根节点(suppliers一个产品一个文件)
            XmlElement item = doc.CreateElement("suppliers");
            doc.AppendChild(item);
        }
        else
        {
            doc.Load(filePath);
        }

        return doc;
    }

    /// <summary>
    /// 填充XML
    /// </summary>
    /// <param name="doc">doc</param>
    /// <param name="dic">dic</param>
    private void FullXMLNode(ref XmlDocument doc,Dictionary<string,string> dic)
    {
        CheckNode(doc,dic);
        int id = GetMaxID(doc);
        XmlElement supplier = doc.CreateElement("supplier");
        supplier.SetAttribute("id",id.ToString());
        foreach (var xmlLabel in dic)
        {
            XmlElement el = doc.CreateElement(xmlLabel.Key);
            el.InnerXml = xmlLabel.Value;
            supplier.AppendChild(el);
        }

        doc.ChildNodes[1].AppendChild(supplier);
    }

    /// <summary>
    /// 初始化字典,配置xml标签
    /// </summary>
    /// <returns>Dictionary</returns>
    private Dictionary<string,string> ConfigXmlLabel()
    {
        string company = Request["company"].ToString();
        string linkMan = Request["linkMan"].ToString();
        string phone = Request["phone"].ToString();
        string fax = Request["fax"].ToString();
        string email = Request["email"].ToString();
        string webUrl = Request["webUrl"].ToString();
        string address = Request["address"].ToString();
        string remark = Request["remark"].ToString();
        Regex myRegex = new Regex("\n",RegexOptions.IgnoreCase);
        remark = myRegex.Replace(remark,"\\n");

        Dictionary<string,string> dictTitle = new Dictionary<string,string>();
        dictTitle.Add("company",company);
        dictTitle.Add("linkMan",linkMan);
        dictTitle.Add("phone",phone);
        dictTitle.Add("fax",fax);
        dictTitle.Add("email",email);
        dictTitle.Add("webUrl",webUrl);
        dictTitle.Add("address",address);
        dictTitle.Add("remark",remark);
        dictTitle.Add("applyDate",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
        return dictTitle;
    }

    /// <summary>
    /// 当前公司填写该信息,这本次填写覆盖之前信息。
    /// </summary>
    /// <param name="doc">xml文件</param>
    /// <param name="dic">数据字典DIC</param>
    private void CheckNode(XmlDocument doc,string> dic)
    {
        string strSelectNodes = string.Format("//suppliers/supplier[company='{0}'][linkMan='{1}'][phone='{2}']",dic["company"].ToString(),dic["linkMan"].ToString(),dic["phone"].ToString());
        XmlNodeList nodelst = doc.SelectNodes(strSelectNodes);  // 判断是否已经存在相同纪录
        if (nodelst.Count > 0)
        {
            foreach (XmlNode node in nodelst)
            {
                node.ParentNode.RemoveChild(node);
            }
        }
    }

    /// <summary>
    /// 获取xml文件属性ID的最大值。
    /// </summary>
    /// <param name="doc">doc</param>
    /// <returns>返回属性ID的最大值</returns>
    private int GetMaxID(XmlDocument doc)
    {
        XmlNodeList nodelst = doc.SelectNodes("//suppliers/supplier/@id[not(//supplier/@id > .)]");  // 获取属性id的最大值
        return nodelst.Count > 0 ? (Convert.ToInt32(nodelst.Item(0).Value) + 1) : 1;
    }
}
原文链接:https://www.f2er.com/xml/299768.html

猜你在找的XML相关文章