tags:

views:

35

answers:

2

i have sample 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>

I have to merge 2 nodes ( with coma seperated) when enddates are same . for the above input i shoud get fallowing out put .

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

I tried using agrigate funtion but how am not able to add end date condition can any boday help

A: 

google XML Serialization in C#.

  1. Deserialize the xml file to List as example.
  2. Do your logic "remove some record or create new merged list"
  3. Serialize the new list to xml file

or

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);
}
Waleed A.K.
Yes thats what i was doing right now but can it be done using Linq functions like grigate or concat?
Questionevrything
I am actually looking for manipulation of Xelemnt using LINQ
Questionevrything
yes you can do it with linq by using Group By, then loop throw each group and do concatenation.
Waleed A.K.
A: 

Something along the following lines may get you started (illustrative - not production quality):

// 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()))));
dommer
thx for this i will take this idea but isnt it like we are doing 2 loops 1 for gruping and then loop2 for merging . my idea is while groupingitself can we add an agrigate ?
Questionevrything
var kk = servicestags.Elements("ServiceTag").Select(T => { var values = T.Elements().Aggregate( (a1,a2)=> {a1.Element("enddate") == a2.Element("enddate") return new XElement("book", "enddate"+"newdate")}) }
Questionevrything