XmlSerializer + Google Base Namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bmomalley@gmail.com

    XmlSerializer + Google Base Namespace

    Hi Folks,

    I need help, because I am just at a loss. I'm not super comfortable
    with XML, but I'm trying to fight my way through generating an RSS XML
    file for Google Base, and I'm stuck with a Namespace issue.

    How can I get XmlSerializer to generate the standard RSS tags with no
    prefix, but ensure that the Google specific tags a prefixed with "g:"?

    Here's a sample of the code I'm using, and it puts g: in front of ALL
    the tags (rss, title, everything):

    RSS.vb

    Imports System.Xml.Seri alization

    ' Taken from http://www.philweber.com/articles/easy_rss_in_vbnet.htm
    Public Class rss
    <XmlElementAttr ibute("channel" )_
    Public channel As New rssChannel()
    <XmlAttributeAt tribute("versio n")_
    Public version As String = "2.0"
    End Class

    Public Class rssChannel
    Public title As String
    Public link As String
    Public description As String
    Public language As String = "en-us"
    <XmlElementAttr ibute("item")_
    Public item As New rssChannelItems ()
    End Class

    Public Class rssChannelItems
    Inherits CollectionBase

    Public Sub Add(ByVal Item As rssChannelItem)
    Dim I As Integer = List.Add(Item)
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As
    rssChannelItem
    Get
    Return CType(List.Item (Index), rssChannelItem)
    End Get
    End Property
    End Class

    Public Class rssChannelItem
    ' Required
    Public id As String
    Public title As String
    Public description As String
    Public link As String
    Public price As Double

    ' Recommended
    Public provider_class As String
    End Class

    Export.vb

    Sub GenerateGoogleB ase()
    Dim rssFeed As rss = Nothing
    Dim rssItem As rssChannelItem

    Try
    rssFeed = New rss

    With rssFeed.channel
    .title = "one"
    .description = "two"
    .link = "three"
    End With

    rssItem = new rssChannelItem
    rssItem.id = "1234"
    rssFeed.channel .item.Add(rssIt em)

    Dim xml As New XmlSerializer(G etType(rss), "http://
    base.google.com/ns/1.0")
    Dim strFile As New FileStream("C:\ Test.xml", FileMode.Create )

    Dim xmlns As New XmlSerializerNa mespaces()
    xmlns.Add("g", "http://base.google.com/ns/1.0")

    xml.Serialize(s trFile, rssFeed, xmlns)

    Catch ex As Exception
    End Try
    End Sub


    What am I missing? I'm sure it's something simple, but I have
    tinkered for hours now and I can't figure it out.

    THANK YOU in advance for any light you can shed on this...

    - Bryan
  • Martin Honnen

    #2
    Re: XmlSerializer + Google Base Namespace

    bmomalley@gmail .com wrote:
    I need help, because I am just at a loss. I'm not super comfortable
    with XML, but I'm trying to fight my way through generating an RSS XML
    file for Google Base, and I'm stuck with a Namespace issue.
    >
    How can I get XmlSerializer to generate the standard RSS tags with no
    prefix, but ensure that the Google specific tags a prefixed with "g:"?
    Can you post a minimal but complete sample of the XML you want to create
    so that we can see which elements you want in the Google namespace?

    Generally if you want to ensure certain elements are in a certain
    namespace then you do that by defining the Namespace =
    "http://base.google.com/ns/1.0" in the XmlElementAttri bute attribute e.g.

    <XmlElement(Nam espace:="http://base.google.com/ns/1.0")_
    Public price As Double

    then make sure the root rss is in no namespace with e.g.

    <XmlRoot(Elemen tName:="rss", Namespace:="")_
    Public Class rss

    then I get (using the your remaining code unchanged)

    <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
    <channel>
    <title>one</title>
    <link>three</link>
    <description>tw o</description>
    <language>en-us</language>
    <item>
    <id>1234</id>
    <g:price>0</g:price>
    </item>
    </channel>
    </rss>


    --

    Martin Honnen --- MVP XML

    Comment

    • bmomalley@gmail.com

      #3
      Re: XmlSerializer + Google Base Namespace

      On May 23, 1:27 pm, Martin Honnen <mahotr...@yaho o.dewrote:
      Generally if you want to ensure certain elements are in a certain
      namespace then you do that by defining the Namespace =
      "http://base.google.com/ns/1.0" in the XmlElementAttri bute attribute e.g.
      >
      <XmlElement(Nam espace:="http://base.google.com/ns/1.0")_
      Public price As Double
      >
      then make sure the root rss is in no namespace with e.g.
      >
      <XmlRoot(Elemen tName:="rss", Namespace:="")_
      Public Class rss
      Yes! This is exactly what I was looking for. Thank you, thank you,
      thank you!

      I knew it would be something simple like this.

      Ironically, I had tried something similar to this a few hours ago, but
      I ended up with:

      <price xmlns:="http://base.google.com/ns/1.0">0</price>

      instead of

      <g:price>0</g:price>

      I had to play around with the code a bit, and I believe it was because
      I had:

      xmlns.Add(Strin g.Empty, String.Empty)

      instead of

      xmlns.Add("g", "http://base.google.com/ns/1.0")

      In any event, thank again!

      - Bryan

      Comment

      Working...