开发者

Linq to XML adding to List Problem send to repeater

I need to get data from a XML file from Node1>Node2

Filter data to group from the xml by X Node where X node is equals A,Y,Z..

Group all elements with same X Node value ordered by date

Store first element of the group(as ordered by date would be by the latest), and the rest in a subgroup of the same object.

If in the filter fails to find one Node equals(A,X or Z) then it creates an first element with hardcoded data for the fist element.

Once this process is finish then send the object (list<>) to a repeater.

I have nearly everything but I don’t know how to do it for when the filter fails to find a Node that is equals to (A,X or Z)

Code Example.

   private void BindOutputRepeaterLongTermCondition()
                {

          XDocument xd = XDocument.Parse(element.OuterXml);

                if (xd != null)
                {

                    var results = (from el1 in xd.Descendants("Node1")
                                   from el2 in el1.Descendants("Node2")
                                   where el2.Element("Date") != null    
                               &&(el2.Element("Code").Value.StartsWith("A") 
                                   || el2.Element("Code").Value.StartsWith("Y")
                                   || el2.Element("Code").Value.StartsWith("Z")
                                   || el2.Element("Code").Value.Sta开发者_如何学PythonrtsWith("B")
                                   )
                                   orderby el2.Element("Date").Value descending
                                   group el2 by el2.Element("Code").Value
                                       into CodeGroup
                                       select new Table(CodeGroup.First())
                                       {
                                           SubTable = (from subCodes in CodeGroup
                                                           select new Table(subCodes)).Skip(1).ToList()
                                       }).ToList<Table>();




                     this.repeaterTable.DataSource = results;
                     this.repeaterTable.DataBind();


                }

            }



            protected void repeater_Table_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
            {
                RepeaterItem item = e.Item;
                if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
                {
                    var recentTable = (Table)item.DataItem;
                    var TableRepeater = (Repeater)item.FindControl("rptSubTable");
                    TableRepeater.DataSource = recentTable.SubTable;
                    TableRepeater.DataBind();
                }
            }


        }

        public class Table
        {
            public string Date { get; set; }
            public string Rubric { get; set; }
            public string Value { get; set; }
            public List<Table> SubTable { get; set; }



            public Table(XElement xml)
            {


                Date = xml.Element("Date") != null ? xml.Element("Date").Value : "";
                Code = xml.Element("Code") != null ? xml.Element("Code").Value : "";

                var a = (string)xml.Element("Code").Value;

                if (a.StartsWith("A"))
                {
                    Name = "Row 1";
                }
                else if (a.StartsWith("Y"))
                {
                    Name = "Row 2";
                }
                else if (a.StartsWith("Z"))
                {
                    Name = "Row 3";
                }

                else if (a.StartsWith("B"))
                {
                    Name = "Row 4";
                }

//Tried the following but it does not work.
                else if (!a.StartsWith("A") && !a.StartsWith("Y") && !a.StartsWith("B")){
                 Name = "Row 3";
                 Value = "Not Found";
                 }
              }

        }

XML Code example

<Node1>
    <Node2>
        <Date>2009-07-16</Date>
        <Code>A</Code>
        <Value>Some Value</Value>
    </Node2>
    <Node2>
        <Date>2008-02-09</Date>
        <Code>Y</Code>
        <Value>Some Value</Value>
    </Node2>
    <Node2>
        <Date>2008-02-09</Date>
        <Code>Z</Code>
        <Value>Some Value</Value>           
    </Node2>
    <Node2>
        <Date>2008-07-16</Date>
        <Code>A</Code>
        <Value>Some Value</Value>
    </Node2>
    <Node2>
        <Date>2006-02-09</Date>
        <Code>Y</Code>
        <Value>Some Value</Value>
    </Node2>
    <Node2>
        <Date>2001-02-09</Date>
        <Code>Z</Code>
        <Value>Some Value</Value>           
    </Node2>
 </Node1>

Any help would be appreciated.

Update The problem is that I need to display all elements whether they are on the xml or not. At the moment I can only display existing elements. If one of the elements (by "Code") is not found I need to display hardcoded name of the element and value "No Value".


Instead of the final elseif:

else if (!a.StartsWith("A") && !a.StartsWith("Y") && !a.StartsWith("B")){
                 Name = "Row 3";
                 Value = "Not Found";
                 }

Have you tried

else
{
Name = "Row n";
Value = "Not Found";
}

It should just fall through to that branch if all other conditions are not met (unless I am missing something)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜