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 / .NET Framework / XML / May 2008

Tip: Looking for answers? Try searching our database.

XmlSerializer + Google Base Namespace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bmomalley@gmail.com - 23 May 2008 18:04 GMT
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.Serialization

' Taken from http://www.philweber.com/articles/easy_rss_in_vbnet.htm
Public Class rss
 <XmlElementAttribute("channel")> _
 Public channel As New rssChannel()
 <XmlAttributeAttribute("version")> _
 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"
 <XmlElementAttribute("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 GenerateGoogleBase()
   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(rssItem)

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

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

     xml.Serialize(strFile, 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 - 23 May 2008 18:27 GMT
> 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 XmlElementAttribute attribute e.g.

    <XmlElement(Namespace:="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(ElementName:="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>two</description>
    <language>en-us</language>
    <item>
      <id>1234</id>
      <g:price>0</g:price>
    </item>
  </channel>
</rss>

Signature

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

bmomalley@gmail.com - 23 May 2008 18:50 GMT
> Generally if you want to ensure certain elements are in a certain
> namespace then you do that by defining the Namespace =
[quoted text clipped - 7 lines]
> <XmlRoot(ElementName:="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(String.Empty, String.Empty)

instead of

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

In any event, thank again!

  - Bryan

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.