Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Google Base XML C#.

Posted on 2009-05-06
9
Medium Priority
?
846 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Accepted Solution

by:
abel earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

577 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