views:

254

answers:

2

This has been annoying me for sometime and I cannot figure out why. I need someone with a keen eye to spot where I could have gone wrong. The exception is thrown by this:

        public static string[] getKeywords(string filename)  
        {  
            var xmlFile = new XElement(filename);  
            string[] keywords = xmlFile.Elements("Keyword").Attributes("name").Select(n => n.Value).ToArray();  
            return keywords;  
        } 

This is the exception:
System.Xml.XmlException was unhandled Message=The '/' character, hexadecimal value 0x2F, cannot be included in a name. Source=System.Xml LineNumber=0 LinePosition=0 StackTrace: at System.Xml.ValidateNames.ThrowInvalidName(String s, Int32 offsetStartChar, Int32 offsetBadChar) at System.Xml.ValidateNames.ParseNCNameInternal(String s, Boolean throwOnError) at System.Xml.XmlConvert.VerifyNCName(String name) at System.Xml.Linq.XName..ctor(XNamespace ns, String localName) at System.Xml.Linq.XNamespace.GetName(String localName, Int32 index, Int32 count) at System.Xml.Linq.XNamespace.GetName(String localName) at System.Xml.Linq.XName.Get(String expandedName) at System.Xml.Linq.XName.op_Implicit(String expandedName) at WinformsHost.Method.getKeywords(String filename) in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\Method.cs:line 16 at WinformsHost.Method.SyntaxHighlight(SyntaxRichTextBox textbox, Language language) in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\Method.cs:line 40 at WinformsHost.MainWindow.rbtCSharp_Checked(Object sender, RoutedEventArgs e) in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\MainWindow.xaml.cs:line 36 at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.Controls.RadioButton.OnChecked(RoutedEventArgs e) at System.Windows.Controls.Primitives.ToggleButton.OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, OperationType operationType) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, OperationType operationType, Boolean isInternal) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Controls.Primitives.ToggleButton.set_IsChecked(Nullable 1 value) at System.Windows.Controls.RadioButton.OnToggle() at System.Windows.Controls.Primitives.ToggleButton.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at WinformsHost.App.Main() in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\obj\x86\Debug\App.g.cs:line 0 InnerException:

+5  A: 

new XElement(filename) means create an element with the name from filename - do you mean XElement.Load(filename) ??

Marc Gravell
+2  A: 

You were trying to load the file name as XML hence it was throwing an exception. This is what you wanted;

    public static string[] getKeywords(string filename)
    {
        var xmlFile = XElement.Load(filename);
        string[] keywords = xmlFile.Elements("Keyword").Attributes("name").Select(n => n.Value).ToArray();
        return keywords;
    }

Using the XElement.Load() method.

Mark Milbourne