我有一个像这样的内存数据表。
我想生成这样的xml文件。
URL(例如http:\www.mymusicwebsite.com\songs)将始终相同。 谁能教我如何实现这一目标?
非常感谢。
L
最佳答案
使用 LINQ 到 XML:
// test items set, you need change to the column accessor
// like dataTable.Rows[2] or dataTable.Rows["fileName"]
// or row.Field<string>("filename") as noted by Mentoliptus
IList<string> items = new List<string> { "song1.mp3", "song2.mp3", "song3.mp3" };
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
// You can output to file or even in a string
using (var stream = File.Create(@"C:\test.xml"))
using (XmlWriter xw = XmlWriter.Create(stream, xws))
{
var xml = new XElement("smil",
new XElement("body",
new XElement("seq",
items.Select(c => new XElement("media",
new XAttribute(
"src",
String.Format("http:\\\\www..mysite.com\\songs\\{0}", c)))))));
xml.Save(xw);
}
输出:
<smil>
<body>
<seq>
<media src="http:\\www..mysite.com\songs\song1.mp3" />
<media src="http:\\www..mysite.com\songs\song2.mp3" />
<media src="http:\\www..mysite.com\songs\song3.mp3" />
</seq>
</body>
</smil>
关于asp.net - 如何通过少量调整将数据表的列写入 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973663/