0

I have a problem parsing the translated text from Microsoft Translator (Windows Azure). I followed the example from here, however, when I try to display the translated text in a VS XAML textbox, the output is: System.Data.Services.Client.QueryOperationResponse`1[Microsoft.Translation].

The submitted query is correct, but when I type it inside a browser, it does not return the translation on screen (it just shows the text "Translation" and the submitted time), but the page source gives an XML document, with correct translation inside a Text tag.

This is my C# code:

var serviceRootUri = new Uri("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/");
var accountKey = "correct account key";
TranslatorContainer tc = new TranslatorContainer(serviceRootUri);
tc.Credentials = new NetworkCredential(accountKey, accountKey);

var translationQuery = tc.Translate(NameInput.Text, "en", "es");
textBox1.Text = translationQuery.Execute().ToString();

The page source (XML output):

> <feed xmlns:base="https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/Translate"
> xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
> xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
> xmlns="http://www.w3.org/2005/Atom">
> <title type="text" />
> <subtitle type="text">Microsoft Translator</subtitle>
> <id>https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/Translate?Text='Mundo'&amp;To='en'&amp;From='es'&amp;$top=100</id>
> <rights type="text" />
> <updated>2012-04-18T10:02:42Z</updated>
> <link rel="self" href="https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/Translate?Text='Mundo'&amp;To='en'&amp;From='es'&amp;$top=100"/>
> <entry>
> <id>https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/Translate?Text='Mundo'&amp;To='en'&amp;From='es'&amp;$skip=0&amp;$top=1</id>
> <title type="text">Translation</title>
> <updated>2012-04-18T10:02:42Z</updated> 
> <link rel="self" href="https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/Translate?Text='Mundo'&amp;To='en'&amp;From='es'&amp;$skip=0&amp;$top=1"/>   
> <content type="application/xml"> 
> <m:properties> <d:Text m:type="Edm.String">World</d:Text> </m:properties>
> </content>
> </entry>
> </feed>

I tried extracting the translated text from XML, following adapted code from here, here and here, as well as Linq, but it does not read from a file that is not saved. With the deprecated Bing translator, I managed to get the parsed text with the XElement.Parse(translatedText).Value command, which is not working now. Is there a way to read from this document (parse from the page source), or any other way to get the translated text?

Community
  • 1
  • 1
dzookatz
  • 213
  • 4
  • 17

1 Answers1

1

The output you're getting there looks like a feed. The .NET framework already has a class that allows you to work easily with feeds, namely the SyndicationFeed class. Before building a parser yourself, I suggest you take a look at this class to see if it already fits your needs.

Resource: http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx

Update: Example to parse the XML Output using SyndicationFeed

        var settings = new XmlWriterSettings
        {
          Indent = true,
          IndentChars = " ",
          OmitXmlDeclaration = true,
          Encoding = new UTF8Encoding(false),
        };

        using (var textReader = new StringReader(<YOUR STRING HERE>))
        {
            var xmlReader = XmlReader.Create(textReader);
            var feed = SyndicationFeed.Load(xmlReader);

            foreach (var item in feed.Items)
            {
                using (var tempStream = new MemoryStream())
                {
                    using (var tempWriter = XmlWriter.Create(tempStream, settings))
                    {
                        item.Content.WriteTo(tempWriter, "Content", "");
                        tempWriter.Flush();

                        // Get the content as XML.
                        var contentXml = Encoding.UTF8.GetString(tempStream.ToArray());
                        var contentDocument = XDocument.Parse(contentXml);

                        // Find the properties element.
                        var propertiesName = XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
                        var propertiesElement = contentDocument.Descendants(propertiesName)
                                                               .FirstOrDefault();

                        // Find all text elements.
                        var textName = XName.Get("Text", "http://schemas.microsoft.com/ado/2007/08/dataservices");
                        var textElements = propertiesElement.Descendants(textName);

                        foreach (var textElement in textElements)
                        {
                            Console.WriteLine("Translated word: {0}", textElement.Value);
                        }
                    }
                }
            }
        }
Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • Yes, I saw this class from another, similar post [link](http://stackoverflow.com/a/10205554/1340960), but I am not sure exactly how could I use it? – dzookatz Apr 18 '12 at 14:50
  • Yes, follow this answer: http://stackoverflow.com/questions/2686533/is-there-a-way-to-create-a-syndicationfeed-from-a-string Once you have the SyndicationFeed, you can start iterating through the items. – Sandrino Di Mattia Apr 18 '12 at 14:56
  • Sorry for the late post, I tried to work with the `SyndicationFeed`, as well as adapted code from [link](http://stackoverflow.com/questions/2686533/is-there-a-way-to-create-a-syndicationfeed-from-a-string) and from some more links. Tried with variations of this: `TextReader tr = new StringReader(results); XmlReader xmlReader = XmlReader.Create(tr); SyndicationFeed feed = SyndicationFeed.Load(xmlReader); NameInput.Text = feed.ToString();`, but to no avail. Thanks for your help, though :) `` – dzookatz Apr 19 '12 at 12:59
  • I managed to parse the sample XML Output you posted. See my original answer for the code. – Sandrino Di Mattia Apr 19 '12 at 13:19