Microsoft Translator XML解析

时间:2012-04-18 10:40:23

标签: c# parsing azure xml-parsing translation

我在解析Microsoft Translator(Windows Azure)中的翻译文本时遇到问题。我按照here中的示例进行操作,但是,当我尝试在VS XAML文本框中显示已翻译的文本时,输出为:System.Data.Services.Client.QueryOperationResponse`1 [Microsoft.Translation]。

提交的查询是正确的,但是当我在浏览器中输入它时,它不会在屏幕上返回翻译(它只显示文本“翻译”和提交的时间),但页面源提供了一个XML文档,在Text标记内正确翻译。

这是我的C#代码:

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

页面源(XML输出):

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

我尝试从XML中提取已翻译的文本,遵循来自hereherehere以及Linq的改编代码,但它不会读取非文件保存。使用已弃用的Bing转换器,我设法使用XElement.Parse(translatedText).Value命令获取已解析的文本,该命令现在不起作用。有没有办法从这个文档中读取(从页面源解析),或以任何其他方式获取翻译文本?

1 个答案:

答案 0 :(得分:0)

你到达那里的输出看起来像一个饲料。 .NET框架已经有一个允许您轻松使用feed的类,即 SyndicationFeed 类。在自己构建解析器之前,我建议你看一下这个类,看看它是否已经满足你的需求。

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

更新:使用SyndicationFeed解析XML输出的示例

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