2014-02-13 63 views
0

我試圖從我的XML文檔中提取CommandPrefix的值(將在底部發布),並且在此過程中遇到一些問題。下面是我使用的(在第二行臨危NullReferanceExeption)提取部分XML文檔時出現問題

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

而這裏的XML代碼(有多個命令集的元素,都在不同的語言中刪除了其他人,以節省空間。):

<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

你的XML命名空間的使用,您必須在查詢中使用它太:

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

謝謝你,偉大的工作! – Kevin