码迷,mamicode.com
首页 > Windows程序 > 详细

【C#】C# 操作XML

时间:2018-12-03 23:32:10      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:div   XML   head   class   new   declare   data   amp   from   

private void addBugCommentToDB(string header, string text)
        {
            string xmlContent = myManageDB.getDataFromCell("bugComment", "BugManager", "bugID", myCurrentBugNo);
            XDocument xml = null;

            if (xmlContent != null && xmlContent != "")
            {
                StringReader myReader = new StringReader(xmlContent);
                XDocument xdoc = XDocument.Load(myReader);
                XElement root = xdoc.Element("Tree");
                XElement e = CreateElement(header, text);
                root.Add(e);
                xml = xdoc;
            }
            else
            {
                XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes");
                XDocument xdoc = new XDocument(dec);
                XElement root = new XElement("Tree");
                XElement e = CreateElement(header, text);
                root.Add(e);
                xdoc.Add(root);
                xml = xdoc;
            }
            string sql = "declare @xmlDoc xml;set @xmlDoc =‘" + xml + "‘update BugManager set bugComment = @xmlDoc where bugID =‘" + myCurrentBugNo + "";
            myManageDB.saveXML(sql);
        }

        private XElement CreateElement(string header, string text)
        {
            return new XElement("Node",
                new XAttribute("Name", header),
                new XAttribute("Text", text)
                );
        }


        private void loadBugComment()
        {
            string xmlContent = myManageDB.getDataFromCell("bugComment", "BugManager", "bugID", myCurrentBugNo);
            if (xmlContent != null && xmlContent != "")
            {
                StringReader myReader = new StringReader(xmlContent);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(myReader);
                LoadXml myLoadXml = new LoadXml();
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    string header = node.Attributes["Name"].Value;
                    string text = node.Attributes["Text"].Value;
                    addBugComments(header, text);
                }
            }
        }

 

【C#】C# 操作XML

标签:div   XML   head   class   new   declare   data   amp   from   

原文地址:https://www.cnblogs.com/haizhibin1989/p/10061695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!