-1

I am getting compiler error: 'System.Collections.Generic.List' to 'System.Xml.Linq.XName'.

I was orgininally getting an 'XAttribute' does not contain a definition for 'Trim' and no accessible extension method 'Trim' ...etc.' but I think I figured it out that my quotes were in the wrong place.

What am I doing wrong?

    public static List<Phrase> LoadPhrasesFromXMLFile(string file)
    {
        try
        {
            XDocument xdocument = XDocument.Load(file);
            char[] trim = new char[3] { '\'', '"', ' ' };
            return xdocument.Descendants("Phrase").Select((Func<XElement, Phrase>)(x => new Phrase()
            {
                eventname = (string)x.Attribute("Event".Trim(trim)),
                priority = int.Parse((string)x.Attribute("Priority".Trim(trim))),
                words = x.Descendants("Word").Select((Func<XElement, Word>)(y =>
                {
                    Word word1 = new Word
                    {
                        preferred_text = (string)y.Attribute("Primaries".Trim(trim).ToLower())
                    };
                    List<string> stringList = (string)y.Attribute("Secondaries") == null || string.IsNullOrWhiteSpace((string)y.Attribute("Secondaries"))
                        ? new List<string>()

Fails at this line:

                        : (List<string>)(IEnumerable<string>)(string)y.Attribute("Secondaries".Trim(trim).Replace(" ", "").ToLower().Split(',').ToList());

Cont code:

                    Word word2 = word1;
                    word2.Ssecondaries = stringList;
                    return word1;
                })).ToList<Word>()
            })).ToList<Phrase>();
        }

Error catching:

        catch (Exception ex)
        {
            Sup.Logger("Encountered an exception reading '" + file + "'. It was: " + ex.ToString(), false, true);
        }
        return (List<Phrase>)null;
    }
CoreyAl
  • 1
  • 1
  • 1
    You should make your code more maintainable, not go for one liners. I doubt this is intentional: `"Secondaries".Trim(trim).Replace(" ", "").ToLower().Split(',').ToList()`. So what that does is Trim the word "Secondaries", replaces any spaces in it with empty space, changes it to lower case, splits it with a comma and turns it into a string list. You probably meant to do that on the actual attribute value instead. – steve16351 Jan 05 '19 at 21:31
  • This should be `((string)y.Attribute("Secondaries")).Trim(trim).Replace(" ", "").ToLower().Split(',').ToList();` – Klaus Gütter Jan 06 '19 at 04:43

1 Answers1

0

Welcome to StackOverflow!

First off, consider the first comment in terms of cleaning up some of the general style. The code is very hard to read as well as the question having multiple split code blocks.

The problematic line's syntax error is solved by changing it to the following (there is

    y.Attribute("Secondaries").Value.Trim(trim).Replace(" ", "").ToLower().Split(',').ToList())

You don't need to do any casting as ToList() will already make it a List. That is the end of the exact compiler issue.

In terms of how to make cleaner code, consider making the helper functions:

    // move 'trim' into an accessible memory location
    private string SanitizeInput (string input) 
    { 
        return input.Trim(trim).Replace(" ", "").ToLower();
    }

    // Having a function like this will change your solution code from the line above to:
    SanitizeInput(y.Attributes("Secondaries).Value).Split(',').ToList();
    // This line is much easier to read as you can tell that the XML parsing is happening, being cleaned, and then manipulated.

Another thing to consider, Word.Ssecondaries (it looks like you might have a typo in your parameter name?) is to see if that property can be set to IEnumerable. It'd dangerous to have it be stored as a List due to the potential for any code to change Word.Secondaries. If you don't intend on changing it, IEnumerable will be much safer. If you find IEnumerable satisfies your needs, you can remove the .ToList() in your problematic line and avoid having to allocate a new chunk of memory for your list as well as having faster code with lazily evaluated queries from LINQ

Travis Primm
  • 149
  • 2
  • 3