Solved

Google Base XML C#.

Posted on 2009-05-06
9
814 Views
Last Modified: 2012-05-06
I'm trying to create a XML data feed to insert our product list into Google base.

This is what is being generated:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
  <channel>
    <Title>Site.us Product List</Title>
    <link>http://www.somewhere.ca/</link>
    <description>Site.us Product List</description>
    <item>
      <title><![CDATA[blah blah title]]></title>
      <link><![CDATA[http://www.somewhere.ca/blah.html]]></link>
      <description><![CDATA[ lots of blah blah]]></description>
      <price>1639.00</price>
      <image_link><![CDATA[http://www.somewhere.ca/Images/big/en/blah.jpg]]></image_link>
      <id><![CDATA[BLAH]]></id>
      <payment_accepted>Cash</payment_accepted>
      <payment_accepted>Debit</payment_accepted>
      <payment_accepted>Visa</payment_accepted>
      <payment_accepted>Amex</payment_accepted>
      <payment_accepted>Paypal</payment_accepted>
      <payment_accepted>Mastercard</payment_accepted>
      <brand>BLAH</brand>
      <condition>New</condition>
      <manufacturer>BLAH</manufacturer>
      <mpn><![CDATA[BLAH]]></mpn>
      <product_type><![CDATA[BLAH BLAH BLAH]]></product_type>
    </item>

If you notice all the namespaces ( g:price) are not showing up! Here is the code to generate this XML.

As you can see I am putting the g:Attribute for the required items...but none of them are sticking to the outputted XML.

How can I include namespaces when generating XML files.

Thanks,
Ghost
public static void BuildGoogleBaseProdListXML(List<GBProdList> oGBProdList, string country) {

         XmlDocument doc = new XmlDocument();

         XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

         doc.AppendChild(docNode);
 

         XmlNode rssNode = doc.CreateElement("rss");

         XmlAttribute rssValueAttribute = doc.CreateAttribute("version");

         rssValueAttribute.Value = "2.0";

         rssNode.Attributes.Append(rssValueAttribute);
 

         XmlAttribute rssAttribute = doc.CreateAttribute("xmlns:g");

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

         rssNode.Attributes.Append(rssAttribute);

         doc.AppendChild(rssNode);
 

         XmlNode ChannelNode = doc.CreateElement("channel");

         rssNode.AppendChild(ChannelNode);
 

         XmlNode titleNode = doc.CreateElement("Title");
 

         titleNode.AppendChild(doc.CreateTextNode(country == "US" ? "Site.us Product List" : "Site.us Product List"));

         ChannelNode.AppendChild(titleNode);
 

         XmlNode linkNode = doc.CreateElement("link");

         linkNode.AppendChild(doc.CreateTextNode(country == "US" ? "http://www.somewhere.us/" : "http://www.somewhere.ca/"));

         ChannelNode.AppendChild(linkNode);
 

         XmlNode DescriptionNode = doc.CreateElement("description");

         DescriptionNode.AppendChild(doc.CreateTextNode(country == "US" ? "Site.us Product List" : "Site.ca Product List"));

         ChannelNode.AppendChild(DescriptionNode);
 

         XmlNode itemNode = null;
 

         foreach (GBProdList oGBProd in oGBProdList) {

            itemNode = doc.CreateElement("item");

            ChannelNode.AppendChild(itemNode);
 

            XmlNode ProdTitleNode = doc.CreateElement("title");

            ProdTitleNode.AppendChild(doc.CreateCDataSection(oGBProd.Title));

            itemNode.AppendChild(ProdTitleNode);
 

            XmlNode ProdLinkNode = doc.CreateElement("link");

            ProdLinkNode.AppendChild(doc.CreateCDataSection(oGBProd.WebSiteLink));

            itemNode.AppendChild(ProdLinkNode);
 

            XmlNode ProdDescriptionNode = doc.CreateElement("description");

            ProdDescriptionNode.AppendChild(doc.CreateCDataSection(oGBProd.Description));

            itemNode.AppendChild(ProdDescriptionNode);
 

            XmlNode ProdPriceNode = doc.CreateElement("g:price");

            ProdPriceNode.AppendChild(doc.CreateTextNode(oGBProd.Price.ToString()));

            itemNode.AppendChild(ProdPriceNode);
 

            XmlNode ProdImageLinkNode = doc.CreateElement("g:image_link");

            ProdImageLinkNode.AppendChild(doc.CreateCDataSection(oGBProd.ImageLink));

            itemNode.AppendChild(ProdImageLinkNode);

            

            XmlNode ProdIdNode = doc.CreateElement("g:id");

            ProdIdNode.AppendChild(doc.CreateCDataSection(oGBProd.ID));

            itemNode.AppendChild(ProdIdNode);
 

            XmlNode ProdPay1Node = doc.CreateElement("g:payment_accepted");

            ProdPay1Node.AppendChild(doc.CreateTextNode("Cash"));

            itemNode.AppendChild(ProdPay1Node);
 

            XmlNode ProdPay2Node = doc.CreateElement("g:payment_accepted");

            ProdPay2Node.AppendChild(doc.CreateTextNode("Debit"));

            itemNode.AppendChild(ProdPay2Node);
 

            XmlNode ProdPay3Node = doc.CreateElement("g:payment_accepted");

            ProdPay3Node.AppendChild(doc.CreateTextNode("Visa"));

            itemNode.AppendChild(ProdPay3Node);
 

            XmlNode ProdPay4Node = doc.CreateElement("g:payment_accepted");

            ProdPay4Node.AppendChild(doc.CreateTextNode("Amex"));

            itemNode.AppendChild(ProdPay4Node);
 

            XmlNode ProdPay5Node = doc.CreateElement("g:payment_accepted");

            ProdPay5Node.AppendChild(doc.CreateTextNode("Paypal"));

            itemNode.AppendChild(ProdPay5Node);
 

            XmlNode ProdPay6Node = doc.CreateElement("g:payment_accepted");

            ProdPay6Node.AppendChild(doc.CreateTextNode("Mastercard"));

            itemNode.AppendChild(ProdPay6Node);
 

            XmlNode ProdBrandNode = doc.CreateElement("g:brand");

            ProdBrandNode.AppendChild(doc.CreateTextNode(oGBProd.Brand));

            itemNode.AppendChild(ProdBrandNode);
 

            XmlNode ProdCondtionNode = doc.CreateElement("g:condition");

            ProdCondtionNode.AppendChild(doc.CreateTextNode(oGBProd.Condition));

            itemNode.AppendChild(ProdCondtionNode);
 

            XmlNode ProdManuNode = doc.CreateElement("g:manufacturer");

            ProdManuNode.AppendChild(doc.CreateTextNode(oGBProd.Supplier));

            itemNode.AppendChild(ProdManuNode);
 

            XmlNode ProdMpnNode = doc.CreateElement("g:mpn");

            ProdMpnNode.AppendChild(doc.CreateCDataSection(oGBProd.SupplierCode));

            itemNode.AppendChild(ProdMpnNode);
 

            XmlNode ProdProductTypeNode = doc.CreateElement("g:product_type");

            ProdProductTypeNode.AppendChild(doc.CreateCDataSection(oGBProd.ProdType));

            itemNode.AppendChild(ProdProductTypeNode);

         }
 

         doc.Save(country == "US" ? @"C:\ServiceErrorLogs\MasterProdList.xml" : @"C:\ServiceErrorLogs\MasterProdListCA.xml");

      }

Open in new window

0
Comment
Question by:copyPasteGhost
  • 6
  • 3
9 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24323310
There are several ways to do this. First and foremost, the namespaces are not attributes, which is probably why the attributes that have qualified names referring to a namespace will not work.

Each CreateElement and CreateAttribute also has a namespaced overload:

// short:
doc.CreateElement("elementName", "http://namespacehere");
// long:
doc.CreateElement("prefix", "elementName", "http://namespacehere");

though prefixes don't matter, you can prefer certain namespace prefixes by using the second syntax. Obviously, it is easy if you place the namespace string in a constant string var.

0
 
LVL 39

Expert Comment

by:abel
ID: 24323332
btw: once you create an element with a prefix, the xml writer will automatically make sure that the declarations for the namespace will be put in the right position (i.e., either on or before the element or attribute).
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24324574
thanks I got that part working.

Now I'm getting errors from google saying:

"There is a problem with the character encoding of this attribute."

this is what I have...
<title><![CDATA[iRobot Self-charging White Home Base for Roomba® 500 Series]]></title>

Google renders it as iRobot Self-charging White Home Base for Roomba® 500 Series

There are a few others...


Like: <title><![CDATA[Dimension Engineering Buffered ±2g Dual Axis Accelerometer (ADXL322)]]></title>

Rendering as:
Dimension Engineering Buffered ±2g Dual Axis Accelerometer (ADXL322)

it seems not to like the ± or the ®....what do I do to fix this?

Thanks,
Ghost
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24324675
try to use the numerical entities of these, like &#xAE; for (r). You can find these entities using windows Charmap.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 39

Expert Comment

by:abel
ID: 24325137
This (num entities) is meant as a workaround. If your codepages are set correctly (and they will be, you are using code to make your XML) your XML is valid and the receiving party is obliged to accept that. Apparently they do not...

Btw, why are you use CDATA sections? Just text/value will be fine.
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24325171
I was getting errors with text value...

The XML is valid and it loads fine in internet explorer without any fuss and all the special characters are there as they should be....

When I then take the file and upload it on Google as a data feed. suddenly they don't know what to do about these "special characters..."
0
 
LVL 39

Expert Comment

by:abel
ID: 24325308
are you sure the upload you are using is not mingling the data? Because it looks as if the UTF-8 encoding is somehow not recognized. As an alternative, and without sacrificing usability (though you will sacrifice a bit readability when you read the xml code by hand) you can try to set a different encoding, like "US-ASCII" (always works) or "ISO-8859-1" (also almost always works, and is a bit more readable because less characters have to be escaped).

By the way, did you try my suggestions?
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24325336
I did unfortunatly it take several hours to get a response back from google...so we'll know soon..

I'll keep you posted.

Thanks.
0
 
LVL 39

Expert Comment

by:abel
ID: 24325776
ah, I see. np, I'll see the follow-up when it's there :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now