2

How can I solve the exception generated?

        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 generates this exception:

System.Xml.XmlException was unhandled Message=The '/' character, hexadecimal value 0x2F, cannot be included in a name. Source=System.Xml

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

2 Answers2

5

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

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

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
  • 141
  • 1
  • 9