xml and VB2005

J

Jaime Lucci

Hi everyone!

How can I generate the following xml code from vb2005?

<item>
<title>Sunshine up Ahead</title>
<media:credit role='author'>Peter Jones</media:credit>
<media:content
url="http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
type="audio/mpeg"/>
</item>

I use XmlDocument and XmlNode types variables, but when I create the node
<media:credit> its only creates <credit>, i mean that it only creates the
node with the words after ":". This is my code in VB:


'Create the node
XmlNodo1 = XmlDoc.CreateElement("media:content")

'Add "url" atributte
XmlAtributo = XmlDoc.CreateAttribute("url")
XmlAtributo.Value =
"http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
XmlNodo1.Attributes.Append(XmlAtributo)

'Add "type" atribute
XmlAtributo = XmlDoc.CreateAttribute("type")
XmlAtributo.Value = "audio/mpeg"
XmlNodo1.Attributes.Append(XmlAtributo)

'Add the node to de xml document
XmlNodo.AppendChild(XmlNodo1)



But as I have mention before, it creates
<content
url="http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
type="audio/mpeg"/>
instead of
<media:content
url="http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
type="audio/mpeg"/>
that's what I need.

Any suggestions?

Thanks

Jaime Lucci
(e-mail address removed)
Salta - Argentina
 
M

Martin Honnen

Jaime said:
How can I generate the following xml code from vb2005?

<item>
<title>Sunshine up Ahead</title>
<media:credit role='author'>Peter Jones</media:credit>
<media:content
url="http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
type="audio/mpeg"/>
</item>

That markup is not well-formed, you need to bind the prefix 'media' to a
URL.
Here is an example how to write XML with XmlWriter:

Const ns As String = "http://example.com/2008/media"
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
Using writer As XmlWriter =
XmlWriter.Create("..\..\XMLOutput1.xml", settings)
writer.WriteStartElement("item")
writer.WriteElementString("title", "Sunshine up Ahead")
writer.WriteStartElement("media", "credit", ns)
writer.WriteAttributeString("role", "author")
writer.WriteString("Peter Jones")
writer.WriteEndElement()
writer.WriteStartElement("media", "content", ns)
writer.WriteAttributeString("url",
"http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3")
writer.WriteAttributeString("type", "audio/mpeg")
writer.WriteEndElement()
writer.WriteEndElement()
End Using

Result looks like this:

<?xml version="1.0" encoding="utf-8"?>
<item>
<title>Sunshine up Ahead</title>
<media:credit role="author"
xmlns:media="http://example.com/2008/media">Peter Jones</media:credit>
<media:content
url="http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
type="audio/mpeg" xmlns:media="http://example.com/2008/media" />
</item>

If you want to avoid the duplicated namespace declaration then write out
the namespace declaration explicitly as in

Const ns As String = "http://example.com/2008/media"
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
Using writer As XmlWriter =
XmlWriter.Create("..\..\XMLOutput1.xml", settings)
writer.WriteStartElement("item")
writer.WriteAttributeString("xmlns", "media",
"http://www.w3.org/2000/xmlns/", ns)
writer.WriteElementString("title", "Sunshine up Ahead")
writer.WriteStartElement("media", "credit", ns)
writer.WriteAttributeString("role", "author")
writer.WriteString("Peter Jones")
writer.WriteEndElement()
writer.WriteStartElement("media", "content", ns)
writer.WriteAttributeString("url",
"http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3")
writer.WriteAttributeString("type", "audio/mpeg")
writer.WriteEndElement()
writer.WriteEndElement()
End Using

That way the result is

<item xmlns:media="http://example.com/2008/media">
<title>Sunshine up Ahead</title>
<media:credit role="author">Peter Jones</media:credit>
<media:content
url="http://www.jeroenwijering.com/upload/peterjones_sunshine_lofi.mp3"
type="audio/mpeg" />
</item>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top