当前位置: 动力学知识库 > 问答 > 编程问答 >

XML Element not showing in output when using XML Writer in C#

问题描述:

I'm trying to create a simple xml file in C# that is a Playlist for vlc media player. I'm new to xml and I just can't seem to figure out why the tracklist element is not showing up in the output. The ending tag for tracklist shows up but no beginning tag. What am I doing wrong?

Here is my code:

 XmlTextWriter xmldoc = new XmlTextWriter(@"C:\Users\Jamie\Documents\Playlist.xspf", Encoding.UTF8);

xmldoc.Formatting = Formatting.Indented;

xmldoc.Indentation = 2;

xmldoc.WriteStartDocument(true); //<xml start>

xmldoc.WriteStartElement("Playlist"); //<Playlist version="1" xmlns="http://xspf.org/ns/0/" xmlns:vls="http://www.videolan.org/vlc/playlist/ns/0/">

xmldoc.WriteStartAttribute("version");

xmldoc.WriteValue(1);

xmldoc.WriteEndAttribute();

xmldoc.WriteStartAttribute("xmlns");

xmldoc.WriteValue("http://xspf.org/ns/0/");

xmldoc.WriteEndAttribute();

xmldoc.WriteStartAttribute("xmlns:vls");

xmldoc.WriteValue("http://www.videolan.org/vlc/playlist/ns/0/");

xmldoc.WriteEndAttribute();

xmldoc.WriteStartElement("title"); //<title>

xmldoc.WriteString("Playlist"); //Playlist

xmldoc.WriteEndElement(); //</title>

xmldoc.WriteStartElement("tracklist"); //<tracklist>

xmldoc.WriteEndElement(); //</tracklist>

xmldoc.WriteEndElement(); //</Playlist>

xmldoc.WriteEndDocument(); //<xml end>

xmldoc.Dispose();

Here is the output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Playlist version="1" xmlns="http://xspf.org/ns/0/"xmlns:vls="http://www.videolan.org/vlc/playlist/ns/0/">

<title>Playlist</title>

<tracklist />

</Playlist>

网友答案:

The output is correct: <tracklist /> denotes an empty tag.

分享给朋友:
您可能感兴趣的文章:
随机阅读: