Groups | Blog | Home
all groups > dotnet xml > november 2005 >

dotnet xml : xmlns:g - base.google.com namespace problem


Kal
11/29/2005 10:33:49 AM
I found some sample code to construct an rss feed and am trying to modify it
to match Google's specs at: http://www.google.com/base/rss_specs.html, but
without knowing much about XML.
Their samples show the namespace declaration in the rss tag, I tried it both
ways with the same result.

It seems reasonably clear, add a namespace:
XmlElement docNode = rssDoc.CreateElement("rss");

XmlAttribute attr = rssDoc.CreateAttribute("version");

attr.Value = "2.0";

docNode.Attributes.Append(attr);

XmlAttribute attr1 = rssDoc.CreateAttribute("xmlns:g");

attr1.Value = "http://base.google.com/ns/1.0";

docNode.Attributes.Append(attr1);

rssDoc.AppendChild(docNode);

Which produces this:

<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">

A bit further along I attempt to use the namespace:

workNode = rssDoc.CreateElement("g:currency");

workNode.InnerText = thisItem.Currency;

itemNode.AppendChild(workNode);

workNode = rssDoc.CreateElement("g:price");

workNode.InnerText = thisItem.Price;

itemNode.AppendChild(workNode);

Which works fine except that it strips out the "g:" and produces this:

<currency>USD</currency>
<price>$20.00</price>

I In the debugger it shows g as the Prefix and currency as the LocalName and
g:currency as Name. The default output is clearly LocalName, but it should
be Name. How do I do it?

Kal




Kal
11/29/2005 1:05:44 PM
Thanks, that worked fine.
Kal

[quoted text, click to view]

Martin Honnen
11/29/2005 7:53:55 PM


[quoted text, click to view]

If you want to create an element in a namespace with the DOM in .NET
then use a namespace aware method e.g.
rssDoc.CreateElement("prefix:localname", "namespaceURI")
or e.g.
rssDoc.CreateElement("prefix", "localname", "namespaceURI")

If you want to create an attribute in a namespace with the DOM in .NET
then use a namespace aware method e.g.
elementNode.SetAttribute("localname", "namespaceURI",
"attribute value")
or e.g.
attributeNode = rssDoc.CreateAttribute("prefix:localname",
"namespaceURI");
attributeNode.Value = "attribute value";
elementNode.SetAtttributeNode(attributeNode);

You usually do not have to create xmlns attributes in the DOM as the
serializer will add them when serializing nodes properly created as
shown above. However if you want to add an xmlns attribute then make
sure you use e.g.
atttributeNode = rssDoc.CreateAttribute("xmlns:prefix",
"http://www.w3.org/2000/xmlns/");
attributeNode.Value = "namespaceURI";
as the namespace URI for the xmlns prefix is predefined as
http://www.w3.org/2000/xmlns/.

--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button