一般处理程序代码: 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);
标签:Xml,AppendChild,c#,写入,secondElement,XmlElement,CreateElement,xMeeting,xmldoc From: https://www.cnblogs.com/okeyl/p/17174527.html