如何根据xelement中的条件合并元素?

时间:2010-06-24 07:19:19

标签: c# linq

我有样本xelement:

<Books>
  <Book>
    <Id>123</Id>
    <EndDate>01/11/2009</EndDate>
    <Price>$0.00</Price>
    <tag1>0</tag1>
    <tag2>0</tag2>
   </Book>
   <Book>
    <Id>567</Id>
    <EndDate>01/01/2001</EndDate>
    <Price>$0.00</Price>
    <tag1>1</tag1>
     <tag2>2</tag2>
   </Book>
   <Book>
    <Id>567</Id>
    <EndDate>01/01/2001</EndDate>
     <tag1>22</tag1>
     <tag2>33</tag2>
    <Price>$0.00</Price>
    </Book>
</Books>

当enddates相同时,我必须合并2个节点(与coma分离)。对于上面的输入我不得不放弃。

<Books>
  <Book>
    <Id>123</Id>
    <EndDate>01/11/2009</EndDate>
    <Price>$0.00</Price>
    <tag1>0</tag1>
    <tag2>0</tag2>
   </Book>
   <Book>
    <Id>567</Id>
    <EndDate>01/01/2001</EndDate>
    <Price>$0.00</Price>
    <tag1>1,22</tag1>
     <tag2>2,33</tag2>
   </Book>
  </Books>

我尝试使用聚合函数,但我无法添加结束日期条件。

2 个答案:

答案 0 :(得分:0)

使用C#进行谷歌XML序列化。

  1. 将xml文件反序列化为List作为示例。
  2. 您的逻辑“删除一些记录或创建新的合并列表”
  3. 将新列表序列化为xml文件
  4. public class Book
    {
      public int Id { get; set; }
      public Decimal Price { get; set; }
      public DateTime EndDate{ get; set; }
      public string tag1{ get; set; }
      public string tag1{ get; set; }
    }
    ....
    
    XDocument xmlDoc = XDocument.Load("Books.xml");
    List<Book> BookList1=
      (from Book in xmlDoc.Descendants("Book")
       select new Tutorial
       {
         Id = tutorial.Element("Id").Value,
         Price = tutorial.Element("Price").Value,
         EndDate = DateTime.Parse(tutorial.Element("EndDate").Value),
         tag1= tutorial.Element("tag1").Value,
         tag2= tutorial.Element("tag2").Value
       }).ToList<Tutorial>();
    ....
    var BookGroups = from g in  BookList1 group g by Id into g
    select new{Key=g.Key,Books=g};
    
    List<Book> BookList2= new List<Book>();
    foreach(var g in BookGroups)
    { 
      Book book = new Book();
      book.Id=g.Key;
      foreach(var b in g.Books)
      {
    // put your logic 
      if(b.Price > book.Price) book.Price =b.Price;
      if(b.EndDate > book.EndDate) book.EndDate =b.EndDate;
      book.tag1+=b.tag1+",";
      book.tag2+=b.tag2+",";
     }
     book.tag1= book.tag1.Trim(',');
     book.tag2= book.tag2.Trim(',');
     BookList2.add(book);
    }
    

答案 1 :(得分:0)

以下几行中的内容可能会让您开始(说明性而非生产质量):

// XElement sourceElement contains the original "Books" element.

var matchingBookGroups = 
    from x in sourceElement.Elements("Book")
    group x by string.Format("{0}-{1}", x.Element("Id").Value, x.Element("EndDate").Value) into g
    select new { Key = g.Key, Values = g };

XElement result =
    new XElement("Books",
        from matchingBookElements in matchingBookGroups
        let firstMatchingBookElement = matchingBookElements.Values.First()
        select
            new XElement("Book",
                firstMatchingBookElement.Element("Id"),
                firstMatchingBookElement.Element("EndDate"),
                firstMatchingBookElement.Element("Price"),
                    new XElement("tag1", string.Join(",", matchingBookElements.Values.Elements("tag1").Select(x => x.Value).ToArray())),
                    new XElement("tag2", string.Join(",", matchingBookElements.Values.Elements("tag2").Select(x => x.Value).ToArray()))));