【发布时间】:2014-05-06 16:23:37
【问题描述】:
我目前正在使用 Microsoft Kinect SDK 开发语音识别应用程序,输出应该是遵循一定结构的 XML 文件,例如:
<?xml version="1.0" encoding="UTF-8"?>
<Speech>
<Item Words="hello" Confidence="0,5705749" Semantic="Child" />
<Item Words="goodbye" Confidence="0,7705913" Semantic="Child" />
</Speech>
Item 节点包含所有读取到的 Kinect 语音识别器识别的信息。目标是:每识别一个新词,就会添加一个新的<Item> 节点及其对应的属性。
我在更新过程中遇到问题,即每次我添加一个新节点时,它都会覆盖最后一个节点,最后只有<Item> 节点。我四处搜索并尝试从其搜索结果中应用解决方案,但没有成功。
写入XML文件的函数如下:
void WriteXML(string result, float confidence, string semantic, string typeOfSpeech)
{
try
{
//pick whatever filename with .xml extension
string filename = "XML" + "SpeechOutput" + ".xml";
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(filename);
}
catch (System.IO.FileNotFoundException)
{
//if file is not found, create a new xml file
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("Speech");
xmlWriter.Close();
xmlDoc.Load(filename);
}
XmlNode root = xmlDoc.DocumentElement;
XmlNode lastWord = root.LastChild;
XmlElement childNode = xmlDoc.CreateElement("Item");
childNode.SetAttribute("Words", result);
childNode.SetAttribute("Confidence", confidence.ToString());
childNode.SetAttribute("Semantic", semantic);
root.InsertAfter(childNode, lastWord);
xmlDoc.Save("W:\\" + filename);
}
catch (Exception ex)
{
WriteError(ex.ToString());
}
}
任何帮助将不胜感激!
为了更方便测试,我将代码添加到一个更简单的App中只是为了测试这个方法:
namespace XMLTesting
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string typeOfSpeech;
public MainWindow()
{
InitializeComponent();
WriteXML("test", 1, "semantic", "typeofspeech");
WriteXML("test2", 2, "semantic", "typeofspeech");
}
void WriteXML(string result, float confidence, string semantic, string typeOfSpeech)
{
try
{
//pick whatever filename with .xml extension
string filename = "XML" + "SpeechOutput" + ".xml";
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(filename);
}
catch (System.IO.FileNotFoundException)
{
//if file is not found, create a new xml file
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("Speech");
xmlWriter.Close();
xmlDoc.Load(filename);
}
XmlNode root = xmlDoc.DocumentElement;
XmlNode lastWord = root.LastChild;
XmlElement childNode = xmlDoc.CreateElement("Item");
childNode.SetAttribute("Words", result);
childNode.SetAttribute("Confidence", confidence.ToString());
childNode.SetAttribute("Semantic", semantic);
root.InsertAfter(childNode, lastWord);
xmlDoc.Save("W:\\" + filename);
}
catch (Exception ex)
{
WriteError(ex.ToString());
}
}
void WriteError(string str)
{
//outputBox.Text = str;
}
}
}
这个应用程序的输出文件是:
<?xml version="1.0" encoding="UTF-8"?>
<Speech>
<Item Words="test2" Confidence="2" Semantic="semantic" />
</Speech>
代替:
<?xml version="1.0" encoding="UTF-8"?>
<Speech>
<Item Words="test" Confidence="1" Semantic="semantic" />
<Item Words="test2" Confidence="2" Semantic="semantic" />
</Speech>
【问题讨论】:
-
您的代码对我来说工作正常,并为每次调用
WriteXml在文件末尾添加一个新行。您在哪里以及如何调用该方法?这种方法是唯一一种写入 XML 目标文件的方法吗? -
我在名为 spRecEng_SpeechRecognized() 的方法上调用该方法,每当语音识别应用程序识别给定语音时,都会调用该方法。我想我也会尝试一下,并在一个非常简单的应用程序中测试这个 WriteXML() 方法(我现在将其添加到原始帖子中),但它仍然没有工作......
-
示例应用程序按预期工作!我在生成的 XML 中有两行。