Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

LINQ to XML - joining xml data

$
0
0
I want to connect data from two XML documents in memory.

First xml document - data about books domain - LINQ , C# , ASP.Net ...
Second xml document - Data About Books - Book name and appropriate DomainId .

In memory , I created first xml document -> xdDomain

<Domains>
  <Domain>
    <Id>L001</Id>
    <Name>LINQ</Name>
  </Domain>
  <Domain>
    <Id>C001</Id>
    <Name>C#</Name>
  </Domain>
  <Domain>
    <Id>A001</Id>
    <Name>ASP.Net</Name>
  </Domain>
</Domains>



and my second xml document -> xdBooks

<Books>
  <Book>
    <Name>Teach Yourself LINQ in 24 Hours</Name>
    <DomainId>L001</DomainId>
  </Book>
  <Book>
    <Name>Pro LINQ</Name>
    <DomainId>L001</DomainId>
  </Book>
  <Book>
    <Name>C# Illustated 2010</Name>
    <DomainId>C001</DomainId>
  </Book>
  <Book>
    <Name>Programming with C# 2010</Name>
    <DomainId>C001</DomainId>
  </Book>
  <Book>
    <Name>ASP.Net 4-0 Professional</Name>
    <DomainId>A001</DomainId>
  </Book>
</Books>



my code is ->
 private static void GetBooksInfo()
        {
            XDocument xdDomain = new XDocument(new XElement("Domains",
                                     new XElement("Domain",
                                         new XElement("Id", "L001"),
                                         new XElement("Name", "LINQ")),
                                     new XElement("Domain",
                                         new XElement("Id", "C001"),
                                         new XElement("Name", "C#")),
                                     new XElement("Domain",
                                         new XElement("Id", "A001"),
                                         new XElement("Name", "ASP.Net"))
                                     ));

            XDocument xdBooks = new XDocument(new XElement("Books",
                                                new XElement("Book",
                                                    new XElement("Name", "Teach Yourself LINQ in 24 Hours"),
                                                    new XElement("DomainId", "L001")),
                                                 new XElement("Book",
                                                    new XElement("Name", "Pro LINQ"),
                                                    new XElement("DomainId", "L001")),
                                                  new XElement("Book",
                                                    new XElement("Name", "C# Illustated 2010"),
                                                    new XElement("DomainId", "C001")),
                                                  new XElement("Book",
                                                    new XElement("Name", "Programming with C# 2010"),
                                                    new XElement("DomainId", "C001")),
                                                  new XElement("Book",
                                                    new XElement("Name", "ASP.Net 4-0 Professional"),
                                                    new XElement("DomainId", "A001"))
                                                    ));


            var qry = from b in xdBooks.Descendants("Book")
                      join d in xdDomain.Descendants("Domain")
                      on b.Element("DomainId").Value equals d.Element("Id").Value
                      select new { book = b, domain = d };

            Console.WriteLine("==== Domain ====|=========== Title ==========");

            foreach (var item in qry)
            {
                Console.WriteLine("{0,-16}| {1}", item.domain.Element("Name").Value, item.book.Element("Name").Value);
            }

        }




Result is OK , Output - >


==== Domain ====|=========== Title ==========
LINQ | Teach Yourself LINQ in 24 Hours
LINQ | Pro LINQ
C# | C# Illustated 2010
C# | Programming with C# 2010
ASP.Net | ASP.Net 4-0 Professional


BUT , If I rearrange the structure of xdDomain like ->

<Domains>
  <Domain Id="L001">LINQ</Domain>
  <Domain Id="C001">C#</Domain>
  <Domain Id="A001">ASP.Net</Domain>
</Domains>



and write LINQ to XML query like - >


 var qry = from b in xdBooks.Descendants("Book")
                      join d in xdDomain.Descendants("Domains")
                      on b.Element("DomainId").Value equals d.Element("Domain").Attribute("Id").Value
                      select new { book = b, domain = d };
  Console.WriteLine("==== Domain ====|=========== Title ==========");

            foreach (var item in qry)
            {
                Console.WriteLine("{0,-16}| {1}", item.domain.Element("Domain").Value, item.book.Element("Name").Value);
            }



I Always get result only with LINQ books (Not with C# or ASP.Net) !!!!

Output ->

==== Domain ====|=========== Title ==========
LINQ | Teach Yourself LINQ in 24 Hours
LINQ | Pro LINQ

Part of code -> d.Element("Domain").Attribute("Id") I seem to always return only first attribute (L001 aka LINQ) .

How can I get an old score with the ALL books and ALL domains , and to keep the changes in Domain xml strucure (Domain with Id Attribute).

Does anyone know of a solution and can help me ?

thanks in advance !

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>