zoukankan      html  css  js  c++  java
  • 解析某些特殊格式XML文件时,获取不到根节点问题

    还是在语音识别这块。在读取本地的SRGS的XML后,无法获取到根节点<grammar>。

    下面是SRGS.XML文件(只给出了根节点)

    <?xml version="1.0" encoding="utf-8" ?>
    
    < grammar version="1.0"          
    
    xml:lang="zh-cn"          
    
    root="mediaMenu"          
    
    tag-format="semantics/1.0"          
    
    xmlns="http://www.w3.org/2001/06/grammar"           
    
    xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions">
    < grammar/> 

    对于这个通过以下解析方式,

    StorageFile xmlFile = await StorageFile.GetFileFromApplicationUriAsync
    (new Uri("ms-appx:///SRGSGrammar.xml"));//获取本地文件 string xmlString = await FileIO.ReadTextAsync(xmlFile);
    
    XDocument xml = XDocument.Parse(xmlString); 
    XElement root = xml.Element("grammar");//获取grammar节点

    获取不到根节点grammar, root值为null

    而将xmlns="http://www.w3.org/2001/06/grammar
    改为xmlns:a="http://www.w3.org/2001/06/grammar"

    则能够获取到,这是本身的没有:a的格式才对语音识别需要的正确格式。

    这是因为标准的SRGS中,默认的namespace 是 http://www.w3.org/2001/06/grammar, 我们在定位元素时,是需要指定XNamespace的:

    XDocument xml = XDocument.Parse(xmlString);
    XNamespace nsSRGSG
    = "http://www.w3.org/2001/06/grammar"; XElement root = xml.Element(nsSRGSG + "grammar");//获取grammar节点

    通过上述就能获取到了。

    详见MSDN文档:

    #XNamespace Class http://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace(v=vs.110).aspx

  • 相关阅读:
    MySQL之数据库优化
    cookie和session
    php自动加载
    php函数之strtr和str_replace的区别
    php函数之substr()
    阶段总结(一)
    json和xml
    sqlserver交换数据行中的指定列
    3 宏、条件编译
    5 常量与变量
  • 原文地址:https://www.cnblogs.com/yffswyf/p/4094090.html
Copyright © 2011-2022 走看看