Linq to XML 添加到列表 问题发送到转发器

发布于 2024-09-28 18:50:40 字数 4914 浏览 4 评论 0原文

我需要从 Node1>Node2 的 XML 文件中获取数据

通过 X 节点过滤数据以从 xml 中进行分组,其中 X 节点等于 A、Y、Z..

将具有相同 X 节点值的所有元素按日期排序

存储存储第一个元素组(按日期排序将按最新顺序),其余部分位于同一对象的子组中。

如果在过滤器中找不到等于(A,X或Z)的节点,那么它会创建第一个元素,其中第一个元素的硬编码数据。

一旦该过程完成,则将对象(list<>)发送到转发器。

我几乎拥有一切,但我不知道当过滤器无法找到等于(A,X或Z)

代码示例的节点时该怎么做。

   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.StartsWith("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 代码示例

<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>

如有任何帮助,我们将不胜感激。

更新 问题是我需要显示所有元素,无论它们是否在 xml 上。 目前我只能显示现有元素。 如果未找到其中一个元素(按“代码”),我需要显示该元素的硬编码名称和值“无值”。

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.StartsWith("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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城追梦 2024-10-05 18:50:40

而不是最后的 elseif:

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

你尝试过吗?

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

如果不满足所有其他条件,它应该直接落到该分支(除非我错过了一些东西)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文