使XDocument识别嵌入式SSML

时间:2019-03-21 18:50:16

标签: c# .net ssml

我正在使用文本进行语音转换以向视频教程添加声音。当前,所有文本都在文件中,并被读入C#应用程序中并解析为多个步骤。我想做的是将ssml添加到文本文件,特别是在整个特定指令中(暂停)的能力。我正在使用Cognitive-Speech-TTS中的示例代码。这段代码使用了

private string GenerateSsml(string locale, string gender, string name, string text)
    {
        var ssmlDoc = new XDocument(
                          new XElement("speak",
                              new XAttribute("version", "1.0"),
                              new XAttribute(XNamespace.Xml + "lang", "en-US"),
                              new XElement("voice",
                                  new XAttribute(XNamespace.Xml + "lang", locale),
                                  new XAttribute(XNamespace.Xml + "gender", gender),
                                  new XAttribute("name", name),
                                  text)));


        return ssmlDoc.ToString();
    }

例如,如果我将“文本”设置为

string text = @"During this video we will refer to this as the lens, 
                <break time=""1000ms"" />  this as the headband  
                 <break time=""1000ms"" />, and these as the frame arms 
                <break time=""1000ms"" />. " };
Content = new StringContent(GenerateSsml(inputOptions.Locale, genderValue, inputOptions.VoiceName, text))

它将无法识别嵌入式xml。有没有一种方法可以让XDocument识别文本中的xml。请注意,在实际的应用程序中,文本是从数据文件中填充的。

1 个答案:

答案 0 :(得分:0)

您正在传递字符串,因此LINQ to XML认为您希望将其作为文本节点,并适当地转义文本。

您似乎真的想包含多个节点-一些文本和一些元素。

我建议像这样更改您的public class MyCallingClass extends ActivityBase { private String _result = null; protected void onCreate(Bundle savedInstanceState) { callingMethod(); } private void callingMethod() { MyASyncClass whatever = new MyASyncClass(output -> _result = output); whatever.execute(); } } class MyASyncClass extends AsyncTask<String, String, String> { public interface AsyncResponse { void processFinish(String output); } private AsyncResponse delegate = null; MyASyncClass(AsyncResponse delegate){ this.delegate = delegate; } @Override protected String doInBackground(String... params) { // Does the work return ""; } @Override public void onPostExecute(String result) { delegate.processFinish(result); } }

GenerateSsml

然后将您的调用方法更改为:

private string GenerateSsml(string locale, string gender, string name, IEnumerable<XNode> nodes)
{
    var ssmlDoc = new XDocument(
                      new XElement("speak",
                          new XAttribute("version", "1.0"),
                          new XAttribute(XNamespace.Xml + "lang", "en-US"),
                          new XElement("voice",
                              new XAttribute(XNamespace.Xml + "lang", locale),
                              new XAttribute(XNamespace.Xml + "gender", gender),
                              new XAttribute("name", name),
                              nodes)));
    return ssmlDoc.ToString();
}

如果您真的想使用字符串表示形式,则可以写:

var nodes = new XNode[]
{
    new XText("During this video we will refer to this as the lens,"),
    new XElement("break", new XAttribute("time", "1000ms")),
    new XText(" this as the headband"),
    new XElement("break", new XAttribute("time", "1000ms")),
    new XText(", and these as the frame arms"),
    new XElement("break", new XAttribute("time", "1000ms"))
    new XText("."),
};
Content = new StringContent(
    GenerateSsml(inputOptions.Locale, genderValue, inputOptions.VoiceName, nodes));