C# 按条件将数据写入xml中_羊豆豆_新浪博客

 public static void WriteXml(GameInfo info,GameObject PanelRunRecord)
    {
        DateTime now = DateTime.Now;
        string dataPath = Application.dataPath;
        string url = dataPath + "/XML/DetailList.xml";
        if (!File.Exists(@url))
        {
            File.Create(@url);
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration Declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlNode rootNode = xmlDoc.createElement_x("detaillist");
        }
        XmlDocument doc = new XmlDocument();
        doc.Load(url);  
        XmlElement rootElem = doc.DocumentElement;
        XElement temp = XElement.Load(url);
        XmlNodeList personNodes = rootElem.ChildNodes; 

        if (personNodes.Count > 0)
        {
            bool isExitYear = false;
            bool isExitMonth = false;
            bool isExitDay = false;

            foreach (XmlNode node in personNodes)
            {
                string year = ((XmlElement)node).getElementsByTagName_r("year")[0].InnerText.ToString().Trim();

                string month = ((XmlElement)node).getElementsByTagName_r("month")[0].InnerText.ToString().Trim();

                if (now.Year == int.Parse(year))
                {
                    isExitYear = true;
                }
                if (now.Year == int.Parse(year) && now.Month == int.Parse(month))
                {
                    isExitMonth = true;
                }
                if (((XmlElement)node).getElementsByTagName_r("mlist")!=null)
                {          
                    XmlNodeList mlist = ((XmlElement)node).getElementsByTagName_r("mlist")[0].ChildNodes;
                    if (mlist.Count>0)
                    {
                        foreach (XmlNode k in ((XmlElement)node).getElementsByTagName_r("mlist"))
                        {
                            int date = int.Parse(k.Attributes.GetNamedItem("date").Value);
                            if (now.Year==int.Parse(year)&&now.Month==int.Parse(month)&&now.Day==date)
                            {
                                isExitDay = true;
                            }
                        }
                    }
                }
            }
            //从月写入
            if (isExitYear&&!isExitMonth&&!isExitDay)
            {
                writerItem_year(doc, personNodes ,now,url, info);
            }
            //从天写入
            if (isExitYear&&isExitMonth&&!isExitDay)
            {
                writerItem_month(doc, personNodes, now, url, info); 
            }
            //从天写入
            if (isExitYear && isExitMonth && isExitDay)
            {
                writerItem_day(doc, personNodes, now, url, info);
            }
            
            if (!isExitYear && !isExitMonth && !isExitDay)
            {
                writerItem_0(doc, now, url, info);
            }

        }
        else
        {
            writerItem_0(doc, now, url,info);
        }
        //刷新记录
        //GameInfo.gdatalist.Clear();
        string XML = Application.dataPath + "/XML/";
        InitializeVRPlatform.InitializeGameRunInfo(XML);
        PanelRunRecord.GetComponent().RecordInitialize();

    }
    #region[直接写入]
    ///
    /// Debug.Log("直接写入");
    ///
    ///
    ///
    ///
    static void writerItem_0(XmlDocument doc, DateTime now, string url,GameInfo info)
    { 
        ISFSObject o = new SFSObject();

        XmlNode root = doc.SelectSingleNode("detaillist");
        XmlNode rootNode = doc.createElement_x("detail");
        root.AppendChild(rootNode);

        XmlElement id = doc.createElement_x("id");
        id.InnerText = now.TimeOfDay.ToString();


        XmlElement Year = doc.createElement_x("year");
        Year.InnerText = now.Year.ToString();

        XmlElement month = doc.createElement_x("month");
        month.InnerText = now.Month.ToString();

        rootNode.AppendChild(id);
        rootNode.AppendChild(Year);
        rootNode.AppendChild(month);

        XmlNode mlist = doc.createElement_x("mlist");

        XmlAttribute date = doc.CreateAttribute("date");
        date.Value = now.Day.ToString();
        mlist.Attributes.Append(date);
        rootNode.AppendChild(mlist);

        XmlNode dlist = doc.createElement_x("dlist");

        XmlAttribute gname = doc.CreateAttribute("gname");
        gname.Value =info.GetGname() ;
        dlist.Attributes.Append(gname);

        XmlAttribute startdate = doc.CreateAttribute("startdate");
        startdate.Value = GameInfo.startdate;
        dlist.Attributes.Append(startdate);

        XmlAttribute enddate = doc.CreateAttribute("enddate");
        enddate.Value = GameInfo.enddate;
        dlist.Attributes.Append(enddate);

        XmlAttribute needtime = doc.CreateAttribute("needtime");
        needtime.Value = "VR";

        dlist.Attributes.Append(needtime);
        mlist.AppendChild(dlist);

        doc.Save(url);
    }
    #endregion

    #region[从年写入] 月
    static void writerItem_year(XmlDocument doc,XmlNodeList personNodes, DateTime now,string url, GameInfo info)
    {
        bool flag = false;
        foreach (XmlNode node in personNodes)
        {
            string year = ((XmlElement)node).getElementsByTagName_r("year")[0].InnerText.ToString().Trim();
            string month = ((XmlElement)node).getElementsByTagName_r("month")[0].InnerText.ToString().Trim();
            if (now.Year == int.Parse(year) && now.Month!=int.Parse(month))
            {
                flag = true;
            }
        }

        if (flag)
        {
            writerItem_0(doc,now,url,info);
        }
        doc.Save(url);

    }
    #endregion

    #region[从月写入] 天
    static void writerItem_month(XmlDocument doc, XmlNodeList personNodes, DateTime now, string url, GameInfo info)
    {
        foreach (XmlNode node in personNodes)
        {
            string year = ((XmlElement)node).getElementsByTagName_r("year")[0].InnerText.ToString().Trim();
            string month = ((XmlElement)node).getElementsByTagName_r("month")[0].InnerText.ToString().Trim();
            if (now.Year == int.Parse(year) && now.Month == int.Parse(month))
            {
                XmlElement mlist = doc.createElement_x("mlist");
                XmlAttribute date = doc.CreateAttribute("date");
                date.Value = now.Day.ToString();
                mlist.Attributes.Append(date);
                node.AppendChild(mlist);

                XmlNode dlist = doc.createElement_x("dlist");

                XmlAttribute gname = doc.CreateAttribute("gname");
                gname.Value = info.GetGname();
                dlist.Attributes.Append(gname);

                XmlAttribute startdate = doc.CreateAttribute("startdate");
                startdate.Value = GameInfo.startdate;
                dlist.Attributes.Append(startdate);

                XmlAttribute enddate = doc.CreateAttribute("enddate");
                enddate.Value = GameInfo.enddate;
                dlist.Attributes.Append(enddate);

                XmlAttribute needtime = doc.CreateAttribute("needtime");
                needtime.Value = "VR";

                dlist.Attributes.Append(needtime);
                mlist.AppendChild(dlist);

                doc.Save(url);
            }
        }
    }
    #endregion

    #region[从时间段写入]时间段
    static void writerItem_day(XmlDocument doc, XmlNodeList personNodes, DateTime now, string url, GameInfo info)
    {
        //查找
        foreach (XmlNode node in personNodes)
        {
            string year = ((XmlElement)node).getElementsByTagName_r("year")[0].InnerText.ToString().Trim();

            string month = ((XmlElement)node).getElementsByTagName_r("month")[0].InnerText.ToString().Trim();

           
                if (((XmlElement)node).getElementsByTagName_r("mlist") != null)
                {
                    XmlNodeList mlist = ((XmlElement)node).getElementsByTagName_r("mlist")[0].ChildNodes;
                    if (mlist.Count > 0)
                    {
                        foreach (XmlNode k in ((XmlElement)node).getElementsByTagName_r("mlist"))
                        {
                            int date = int.Parse(k.Attributes.GetNamedItem("date").Value);
                            if (now.Year == int.Parse(year) && now.Month == int.Parse(month) && now.Day == date)
                            {
                                XmlNode dlist = doc.createElement_x("dlist");

                                XmlAttribute gname = doc.CreateAttribute("gname");
                                gname.Value = info.GetGname();
                                dlist.Attributes.Append(gname);

                                XmlAttribute startdate = doc.CreateAttribute("startdate");
                                startdate.Value = GameInfo.startdate;
                                dlist.Attributes.Append(startdate);

                                XmlAttribute enddate = doc.CreateAttribute("enddate");
                                enddate.Value = GameInfo.enddate;
                                dlist.Attributes.Append(enddate);

                                XmlAttribute needtime = doc.CreateAttribute("needtime");
                                needtime.Value = "VR";

                                dlist.Attributes.Append(needtime);
                                k.AppendChild(dlist);
                                doc.Save(url);
                        }
                        }
                    }
                }
            
            
        }

    }
    #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值