Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andrew 19 posts 39 karma points
    Sep 17, 2012 @ 04:15
    Andrew
    0

    Working with TextstringArray in Razor

    Hey everyone,

    I've been trying for a while now to figure out how to get the data out of the uComponents TextstringArray data type. It's stored as XML. Here's the XML from the cmsContentXml table:

    <sermonCSS>
    <TextstringArray>
    <values>
    <value>Color</value>
    <value>Black</value>
    </values>
    <values>
    <value>Border</value>
    <value>1px solid black</value>
    </values>
    </TextstringArray>
    </sermonCSS>

    As a side question, does anyone know why there is an extra node called "TextstringArray" inside my defined property? The other uComponent types (e.g. Multiple Textstring) don't store their data like that.

    Back to my ACTUAL question. I'm having trouble getting to that data using Razor. I'm trying to get the data into javascript variables, but I'd be having the same problems if I were putting it into HTML. Here are some of the things I've tried (inside a script tag):

    var css = [];
    @foreach (var x in Model.sermonCSS){
    @:css.push(@x.value[0].InnerText);
    }
    var css = [];
    @foreach (var x in Model.sermonCSS){
    @:css.push(@x.Value[0].InnerText);
    }
    var css = [];
    @foreach (var x in Model.sermonCSS.TextstringArray){
    @:css.push("@x.Value[0].InnerText");
    }

    Etc. etc. I think you get the point. I've tried just about every combination and can't get anything out of it. Whenver I do console.log(css), it's just an empty array ( [ ] ). Has anyone worked with this data type and know how to get at the data?

    This is using Umbraco 4.7.1 on Windows 7 with .NET 4.0.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 19, 2012 @ 14:41
    Dan Diplo
    1

    I ended up loading the raw XML (stored in the property - in your case this would be Model.sermonCSS) and loading it into an XDocument and then parsing that into a DTO class. Something like this:

    public static List<KeywordData> GetKeywordDataFromReportXml(string xml)
    {
        List<KeywordData> keywords = new List<KeywordData>();
    
        if (!String.IsNullOrEmpty(xml))
        {
            try
            {
                var xdoc = XDocument.Parse(xml);
                var values = xdoc.Root.Elements("values");
    
                if (values != null)
                {
                    foreach (var item in values)
                    {
                        KeywordData kd = new KeywordData()
                        {
                            Keyword = item.Elements().First().Value,
                            Volume = GetElementAsInt(item.Elements(), 1),
                            StartRank = GetElementAsInt(item.Elements(), 2),
                            PreviousRank = GetElementAsInt(item.Elements(), 3),
                            CurrentRank = GetElementAsInt(item.Elements(), 4),
                            PreviousVisits = GetElementAsInt(item.Elements(), 6),
                            CurrentVisits = GetElementAsInt(item.Elements(), 7)
                        };
    
                        keywords.Add(kd);
                    }
                }
            }
            catch (System.Xml.XmlException xmlEx)
            {
                throw new System.Xml.XmlException(String.Format("XML processing error: {0}\n<pre>{1}</pre>", 
                    xmlEx.Message, xml), xmlEx, xmlEx.LineNumber, xmlEx.LinePosition);
            }
        }
    
        return keywords;
    }
    
    private static int GetElementAsInt(IEnumerable<XElement> elements, int position)
    {
        int number = 0;
        var elem = elements.Skip(position).Take(1).FirstOrDefault();
    
        if (elem != null)
        {
            int.TryParse(Regex.Replace(elem.Value, "[^0-9]", String.Empty), out number);
        }
    
        return number;
    }
    
    Obviously the class KeywordData would need to have the properties appropriate for your data. Not straightforward, but once you've done it you can work nicely with a strongly-typed list of values.
Please Sign in or register to post replies

Write your reply to:

Draft