在提取XML文档的一部分时遇到问题

时间:2014-02-13 23:44:43

标签: c# xml windows-phone-8

我正在尝试从我的XML文档中提取CommandPrefix的值(将在底部发布),并且在此过程中遇到了一些问题。这是我正在使用的代码(在第二行接收NullReferanceExeption)

XDocument xDoc = XDocument.Load("voicecmd.xml");
tb.Text = xDoc.Root.Element("CommandSet").Element("CommandPrefix").Value;

这是XML(有多个CommandSet元素,全部使用不同语言。删除其他语言以节省空间):

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
  <CommandSet xml:lang="en-US">
    <CommandPrefix>Phone</CommandPrefix>
    <Example>send an email</Example>
    <Command Name="SendMail">...</Command>
    <Command Name="GetWeather">...</Command>
    <Command Name="Alarm">...</Command>
    <Command Name="Reminder">...</Command>
    <Command Name="ShareStatus">...</Command>
    <Command Name="WhatCanISay">...</Command>
    <Command Name="SearchGoogle">
      <Example>Search Google</Example>
      <ListenFor>[Search] [on] Google [for] {*}</ListenFor>
      <Feedback>Ok, i'll search Google.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="SearchBing">
      <Example>Search Bing</Example>
      <ListenFor>[Search] [on] Bing [for] {*}</ListenFor>
      <Feedback>Ok, i'll search Bing</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="SearchAmazon">
      <Example>Search Amazon</Example>
      <ListenFor>[Search] [on] Amazon [for] {*}</ListenFor>
      <Feedback>Ok, i'll search Amazon.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="SearchYahoo">
      <Example>Search Yahoo</Example>
      <ListenFor>[Search] [on] Yahoo [for] {*}</ListenFor>
      <Feedback>Ok, i'll search Yahoo.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="SearchYouTube">
      <Example>Search YouTube</Example>
      <ListenFor>[Search] [on] YouTube [for] {*}</ListenFor>
      <Feedback>Ok, i'll search YouTube.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Shuffle">
      <Example>Shuffle Music</Example>
      <ListenFor>Shuffle [Music] [Song] [Songs] {*}</ListenFor>
      <ListenFor>Play [a] [random] [song] [music] {*}</ListenFor>
      <Feedback>Okay. I'll shuffle your music for you.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Pause">
      <Example>Pause Music</Example>
      <ListenFor>Pause [the] [my] [Music] [Song] [this] {*}</ListenFor>
      <ListenFor>Stop [the] [my] [Music] [Song] {*}</ListenFor>
      <Feedback>Okay. I'm pausing the currently playing track.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Next">
      <Example>Next Song</Example>
      <ListenFor>Next [song] [track] [music] {*}</ListenFor>
      <ListenFor>Skip [song] [track] [music] {*}</ListenFor>
      <Feedback>I'll play the next song in your library.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Previous">
      <Example>Previous Song</Example>
      <ListenFor>Previous [song] [track] [music] {*}</ListenFor>
      <ListenFor>Back [a] [one] [song] [track] [music] {*}</ListenFor>
      <ListenFor>Reverse [a] [one] [song] [track] [music] {*}</ListenFor>
      <ListenFor>Last [a] [one] [song] [track] [music] {*}</ListenFor>
      <Feedback>I'll go back to the previous song in your library.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Time">
      <Example>What time is it?</Example>
      <ListenFor>[what] time [is] [it] [the] {*}</ListenFor>
      <ListenFor>[what's] [whats] [the] time {*}</ListenFor>
      <ListenFor>[current] [local] time {*}</ListenFor>
      <Feedback>I'll get you the current time.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Date">
      <Example>What's Todays date?</Example>
      <ListenFor>
        [whats] [todays] [the] [current] [is] [what] date {*}
      </ListenFor>
      <ListenFor>[what] date [is] [it] {*}</ListenFor>
      <ListenFor>[current] date {*}</ListenFor>
      <Feedback>I'll get you the current date.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="Maps">
      <Example>Search Maps</Example>
      <ListenFor>maps [search] [my] [a] [open] [for] {*}</ListenFor>
      <ListenFor>search [a] [my] maps [open] [for] {*}</ListenFor>
      <ListenFor>open maps {*}</ListenFor>
      <Feedback>I'm opening a map for you to search.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="MapDirections">
      <Example>Get Directions</Example>
      <ListenFor>[get] directons [to] {*}</ListenFor>
      <ListenFor>map {*}</ListenFor>
      <ListenFor>navigate [to] [a] [location] {*}</ListenFor>
      <ListenFor>take [me] [to] [a] [location] {*}</ListenFor>
      <Feedback>
        I'm getting directions from your current location now.
      </Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="SongInfo">
      <Example>About This Song...</Example>
      <ListenFor>about [the] [currently] [playing] song</ListenFor>
      <ListenFor>about [a] [this] song [named] [called] {*}</ListenFor>
      <Feedback>I'll get that information for you.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="StoreSearch">
      <Example>Search the Store</Example>
      <ListenFor>
        search [the] [windows] [phone] [store] [marketplace] [for] {*}
      </ListenFor>
      <Feedback>I'm searching the store now.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="MusicSearch">
      <Example>Search the Store for Music</Example>
      <ListenFor>
        search [the] [windows] [phone] [store] [for] music [marketplace] {*}
      </ListenFor>
      <Feedback>I'm searching the store for music now.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="BatteryLevel">
      <Example>How much battery do I have left?</Example>
      <ListenFor>
        [how] [much] battery [do][is] [i] [have] [left] [remaining] {*}
      </ListenFor>
      <Feedback>I'll get that information for you.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <Command Name="AppointmentCreate">
      <Example>Create a new appointment</Example>
      <ListenFor>[create] [a] [new] appointment {*}</ListenFor>
      <ListenFor>[create] [a] [new] event {*}</ListenFor>
      <Feedback>Okay, i'll create a new appointment for you.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>
    <!--

    <Command Name ="Calc">
      <Example>What is 5 plus 5</Example>
      <ListenFor>what is {*} </ListenFor>
      <ListenFor>calculate {*}</ListenFor>
      <ListenFor>solve {*}</ListenFor>
      <ListenFor>open calculator</ListenFor>
      <Feedback>I'm opening the calculator...</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>

    <Command Name ="Timer">
      <Example>Set a timer for 5 minutes</Example>
      <ListenFor>[set] [a] timer [for] {*} </ListenFor>
      <ListenFor>[create] [a] [new] timer </ListenFor>
      <Feedback>Ok, i'm creating your timer.</Feedback>
      <Navigate Target="Speech.xaml"/>
    </Command>

-->
  </CommandSet>
</VoiceCommands>

1 个答案:

答案 0 :(得分:1)

您的XML使用命名空间,您也必须在查询中使用它:

var ns = XNamespace.Get("http://schemas.microsoft.com/voicecommands/1.0");
tb.Text = xDoc.Root.Element(ns + "CommandSet").Element(ns + "CommandPrefix").Value;