c# XML 有多个重复子节点操作

1,继续添加ebm子节点

public static void CreateXml()
        {
            //创XML建对象
            XmlDocument doc = new XmlDocument();
            //声明根节点
            XmlElement books;
            //判断文件是否存在
            if (File.Exists("d://Test.xml"))
            {
                //该文件存在
                //加载文件
                doc.Load("d://Test.xml");
                //获得根节点
                books = doc.DocumentElement;

            }
            else//该文件不存在
            {
                //创建声明
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                //创建根节点
                books = doc.CreateElement("EBD");
                doc.AppendChild(books);
            }

            //开始正常写入信息就可以了
            XmlElement xml = doc.CreateElement("EBM");
            books.AppendChild(xml);

            XmlElement ebmId = doc.CreateElement("EBMID");
            ebmId.InnerText = "1";
            xml.AppendChild(ebmId);

            XmlElement areaCode = doc.CreateElement("AreaCode");
            areaCode.InnerText = "360699999901";
            xml.AppendChild(areaCode);

            XmlElement startTime = doc.CreateElement("StartTime");
            startTime.InnerText = "2018-07-26 9:06:04";
            xml.AppendChild(startTime);

            XmlElement endTime = doc.CreateElement("EndTime");
            endTime.InnerText = "2018-07-26 7:22:04";
            xml.AppendChild(endTime);

            XmlElement msgType = doc.CreateElement("MsgType");
            msgType.InnerText = "1";
            xml.AppendChild(msgType);

            doc.Save("d://Test.xml");
        }

2, 修改某个子节点(通过EBMID确定具体的节点)

public static void UpdateXmlForEBMID(string ebmId)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("d://Test.xml");
            var xns = xmlDoc.SelectSingleNode("EBD");
            XmlNodeList xnl = xns.ChildNodes;
            foreach(XmlNode xn in xnl )
            {
                XmlElement xe = (XmlElement)xn;
                XmlNodeList xnl2 = xe.ChildNodes;
                foreach (XmlNode xn2 in xnl2)
                {
                    XmlElement xe2 = (XmlElement)xn2;

                    if (xe2.InnerText.Equals(ebmId))
                    {
                        XmlNodeList elemList = xe.GetElementsByTagName("MsgType");
                        elemList[0].InnerXml = "2";
                    }
                    break;
                }
                //break;//如果更新完了指定节点,有需要则退出循环
            }
            xmlDoc.Save("d://Test.xml");//保存的该XML文件,否则更新无效
        }

3,读取某个子节点下的所有属性值(这边也是通过EBMID去匹配)

public static void ReadXMLForLog(string xmlPath,string ebmId)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("d://Test.xml");
            var xns = xmlDoc.SelectSingleNode("EBD");
            XmlNodeList xnl = xns.ChildNodes;
            Dictionary<string, string> obj = new Dictionary<string, string>();
            
            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                XmlNodeList xnl2 = xe.ChildNodes;

                foreach (XmlNode xn2 in xnl2)
                {
                    XmlElement xe2 = (XmlElement)xn2;
                    if (xe2.InnerText.Equals("2"))
                    {
                        var objs = new {
                            EBMID = xe.GetElementsByTagName("EBMID"),
                            AreaCode = xe.GetElementsByTagName("AreaCode"),
                            StartTime= xe.GetElementsByTagName("StartTime"),
                            EndTime = xe.GetElementsByTagName("EndTime"),
                            MsgType = xe.GetElementsByTagName("MsgType"),
                        };
                        string ebmIdValue = objs.EBMID[0].InnerText;
                    }
                    break;
                }
            }
        }
posted @ 2018-09-04 11:11  Victor_huang  阅读(4616)  评论(0编辑  收藏  举报