简体   繁体   中英

Microsoft Translator XML parse

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?

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);
                        }
                    }
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM