Question

I have the following code generating an XML podcast file for iTunes podcast:

Dim writer As New XmlTextWriter("c:\jdir\test.xml", System.Text.Encoding.UTF8)

writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 4

writer.WriteStartElement("channel")

writer.WriteStartElement("title")
writer.WriteString("My Podcast")
writer.WriteEndElement()
....etc etc..

But when i came to this part:

<itunes:image href="http://www.mywebsitehere.com/student.jpg" />        
<itunes:category text="Education">      
    <itunes:category text="Education Technology"/>      
</itunes:category>      
<itunes:category text="Higher Education"/>

I was unsure how to add the href= to the element itself and also the text=?

Any help would be great!

Was it helpful?

Solution

You can add href= and text = by using WriteAttributeString..

Try the below code.. May be it will help you...

    writer.WriteStartElement("itunes:image")
    writer.WriteAttributeString("href", "http://www.mywebsitehere.com/student.jpg")
    writer.WriteEndElement()
    writer.WriteStartElement("itunes:category")
    writer.WriteAttributeString("text", "Education")
    writer.WriteStartElement("itunes:category", Nothing)
    writer.WriteAttributeString("text", "Education Technology")
    writer.WriteEndElement()
    writer.WriteEndElement()
    writer.WriteStartElement("itunes:category")
    writer.WriteAttributeString("text", "Education Technology")
    writer.WriteEndElement()

OTHER TIPS

Fwiw, with VB, you can simplify this greatly using LINQ to XML and object initializers. Note, you will need to add an imports for the iTunes namespace, but after that you can simply do:

Dim myXml = <channel>
  <itunes:title>My Podcast</itunes:title>
  <itunes:image href="http://www.mywebsitehere.com/student.jpg" />        
  <itunes:category text="Education">      
    <itunes:category text="Education Technology"/>      
  </itunes:category>      
  <itunes:category text="Higher Education"/>
</channel>

MyXml.Save("c:\jdir\test.xml")

If you want to insert a value from a variable, just use the <% SomeValue %> as you would with ASP.Net.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top