c# 写入Xml 元素(<![CDATA[ ]];)

Posted okeyL 探索未知世界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 写入Xml 元素(<![CDATA[ ]];)相关的知识,希望对你有一定的参考价值。

一般处理程序代码:
 XmlDocument xmldoc = new XmlDocument();
            xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null));
            XmlElement firstElement = xmldoc.CreateElement("", "data", "");
            xmldoc.AppendChild(firstElement);
            using (GSParkEntities db = new GSParkEntities())
            
                DateTime date=Convert.ToDateTime( DateTime.Now.ToString("yyyy-MM-dd"));
                IList<Meeting> list = db.Meeting.Where(o => o.MeetDate >= date).OrderBy(o=>o.MeetDate).ToList();
                firstElement.SetAttribute("count", list.Count + "");
                foreach (Meeting xMeeting in list)
                
                    XmlElement secondElement = xmldoc.CreateElement("item");
                    //secondElement.SetAttribute("MeetDate",(xMeeting.MeetDate).ToString("yyyy-MM-dd"));
                    //secondElement.SetAttribute("MeetTilte", xMeeting.MeetTitle + "");
                    //secondElement.SetAttribute("Speaker", xMeeting.Speaker);
                    //secondElement.SetAttribute("MeetTime", (xMeeting.MeetDate).ToString("hh:mm"));
                    //secondElement.SetAttribute("MeetPlace", xMeeting.MeetPlace);
                    XmlElement meetDate = xmldoc.CreateElement("meetDate");
                    meetDate.InnerText = (xMeeting.MeetDate).ToString("yyyy-MM-dd");
                    secondElement.AppendChild(meetDate);
 
                    //XmlElement meetTitle = xmldoc.CreateElement("meetTitle");
                    //meetTitle.InnerText = "<![CDATA[" + xMeeting.MeetTitle + "]]>";
                    //secondElement.AppendChild(meetTitle);
                    XmlElement meetTitle = xmldoc.CreateElement("meetTitle");
                    XmlCDataSection cd = xmldoc.CreateCDataSection(xMeeting.MeetTitle);
                    meetTitle.AppendChild(cd);
                    secondElement.AppendChild(meetTitle);
 
                    //XmlElement speaker = xmldoc.CreateElement("speaker");
                    //speaker.InnerText = "<![CDATA[" + xMeeting.Speaker + "]]>";
                    //secondElement.AppendChild(speaker);
                    XmlElement speaker = xmldoc.CreateElement("speaker");
                    XmlCDataSection cd1 = xmldoc.CreateCDataSection(xMeeting.Speaker);
                    speaker.AppendChild(cd1);
                    secondElement.AppendChild(speaker);
 
                    XmlElement meetTime = xmldoc.CreateElement("meetTime");
                    meetTime.InnerText = (xMeeting.MeetDate).ToString("HH:mm");
                    secondElement.AppendChild(meetTime);
 
                    //XmlElement meetPlace = xmldoc.CreateElement("meetPlace");
                    //meetPlace.InnerText = "<![CDATA[" + xMeeting.MeetPlace + "]]>";
                    //secondElement.AppendChild(meetPlace);
                    XmlElement meetPlace = xmldoc.CreateElement("meetPlace");
                    XmlCDataSection cd2 = xmldoc.CreateCDataSection(xMeeting.MeetPlace);
                    meetPlace.AppendChild(cd2);
                    secondElement.AppendChild(meetPlace);
 
 
                    firstElement.AppendChild(secondElement);
                
                
            
           xmldoc.Save(context.Response.Output);

 

以上是关于c# 写入Xml 元素(<![CDATA[ ]];)的主要内容,如果未能解决你的问题,请参考以下文章

向XML中写入数据

C# 读取XML的CDATA节点内容.

Boost XML 解析器可以支持 <![CDATA[ … ]]> 吗?

C# 解析字符串xml

XML CDATA

xml元素类型PCDATA和CDATA的区别(DTD中)