Google Base XML C#.

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

LVL 13
copyPasteGhostAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
abelConnect With a Mentor Commented:
try to use the numerical entities of these, like &#xAE; for (r). You can find these entities using windows Charmap.
0
 
abelCommented:
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
 
abelCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
copyPasteGhostAuthor Commented:
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
 
abelCommented:
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
 
copyPasteGhostAuthor Commented:
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
 
abelCommented:
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
 
copyPasteGhostAuthor Commented:
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
 
abelCommented:
ah, I see. np, I'll see the follow-up when it's there :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.