简体   繁体   中英

XmlException for the '/' character hex value 0x2F cannot be included in a name

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

new XElement(filename)意味着使用filename的名称创建一个元素-您的意思是XElement.Load(filename)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM