473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.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
Jun 27 '08 #1
2 2309
bm*******@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 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/
Jun 27 '08 #2
On May 23, 1:27 pm, Martin Honnen <mahotr...@yahoo.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 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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
2
by: magister | last post by:
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
4
by: Sebastien Tardif | last post by:
Subject: XmlSerializer.Deserialize complain when root declare the namespace If I do XmlSerializer.Deserialize( myString ) and myString is: String myString = "<?xml version=\"1.0\"...
3
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as...
1
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml...
0
by: theonlydavewilliams | last post by:
Hi there I'm hoping there's an easy answer to a (hopefully) not too long-winded issue... I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.