Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

xml and VB2005

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jaime Lucci - 27 Mar 2008 14:31 GMT
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
jaimelucci@hotmail.com
Salta - Argentina
Martin Honnen - 27 Mar 2008 14:55 GMT
> How can I generate the following xml code from vb2005?
>
[quoted text clipped - 5 lines]
> 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>

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.