Solved

Google Base XML C#.

Posted on 2009-05-06
9
827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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, …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

615 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