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
> 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>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
> 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
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