日期:2014-05-18  浏览次数:20420 次

用ASP.NET生成XML的问题
在做个网站,但是网站的图片显示是用XML的
所以请问下怎么用ASP.NET(C#)生成以下的XML呢?
XML code

<?xml version="1.0" encoding="utf-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
  <trackList>
    <track>
      <title>LAN CLUB'S 2YEAR ANNIVERSARY!!</title>
      <creator>Jeroen Wijering</creator>
      <location>images/01.jpg</location>
      <info></info>
    </track>
    <track>
      <title>WWW.LANRENTUKU.COM</title>
      <creator>Jeroen Wijering</creator>
      <location>images/02.jpg</location>
      <info></info>
    </track>
    <track>
      <title>I AM XIXI</title>
      <creator>Jeroen Wijering</creator>
      <location>images/03.jpg</location>
      <info></info>
    </track>
  </trackList>
</playlist>
  



------解决方案--------------------
linq to xml 你可以了解下这个 操作xml非常方便
------解决方案--------------------
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

namespace CSDNTestWeb.XML文件
{
public partial class XMLWeb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
}

protected void btnXML_Click(object sender, EventArgs e)
{
List<ClassInfo> listInfo = new List<ClassInfo>();
//收集数据
listInfo.Add(new ClassInfo() { title = "LAN CLUB'S 2YEAR ANNIVERSARY!!", creator = "Jeroen Wijering", location = "images/01.jpg", info = "" });
listInfo.Add(new ClassInfo() { title = "WWW.LANRENTUKU.COM", creator = "Jeroen Wijering", location = "images/02.jpg", info = "" });
listInfo.Add(new ClassInfo() { title = "I AM XIXI", creator = "Jeroen Wijering", location = "images/03.jpg", info = "" });
MethodForAction(listInfo, @"/XML文件/XML/");

}

/// <summary>
/// 生成XML文件的方法
/// </summary>
/// <param name="listClassInfo">ClassInfo类的集合</param>
/// <param name="path">XML文件的路径</param>
protected void MethodForAction(List<ClassInfo> listClassInfo, string path)
{
//XMLDocument对象
XmlDocument doc = new XmlDocument();
doc.InnerXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><playlist version=\"paperInfo \" xmlns=\" http://xspf.org/ns/0/ \" ></playlist>";//定义xml标准文档格式
XmlElement root = doc.DocumentElement;//到到根节点
XmlElement trackList = doc.CreateElement("trackList");
root.AppendChild(trackList);
foreach (var item in listClassInfo)
{
//创建track节点
XmlElement track = doc.CreateElement("track");
trackList.AppendChild(track);
//创建title节点
XmlElement title = doc.CreateElement("title");
title.InnerText = item.title;
track.AppendChild(title);
//创建creator节点
XmlElement creator = doc.CreateElement("creator");
creato