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 / November 2005

Tip: Looking for answers? Try searching our database.

xmlns:g - base.google.com namespace  problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kal - 29 Nov 2005 18:33 GMT
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
Martin Honnen - 29 Nov 2005 18:53 GMT
> 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.

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/.

Signature

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

Kal - 29 Nov 2005 21:05 GMT
Thanks, that worked fine.
Kal

>> 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,
[quoted text clipped - 25 lines]
> as the namespace URI for the xmlns prefix is predefined as
> http://www.w3.org/2000/xmlns/.

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.