9 Replies - 1804 Views - Last Post: 26 January 2013 - 01:31 PM Rate Topic: -----

#1 petkovic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-August 11

LINQ to XML - joining xml data

Posted 26 January 2013 - 06:08 AM

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 !

Is This A Good Question/Topic? 0
  • +

Replies To: LINQ to XML - joining xml data

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 06:21 AM

Please post the code that constructs the Id attributes.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 06:40 AM

        private static void GetBooksInfo() {
            XDocument xdDomain = new XDocument(new XElement("Domains",
                                     new XElement("Domain", new XAttribute("Id", "L001"),
                                         new XElement("Name", "LINQ")),
                                     new XElement("Domain", new XAttribute("Id", "C001"),
                                         new XElement("Name", "C#")),
                                     new XElement("Domain", new XAttribute("Id", "A001"),
                                         new XElement("Name", "ASP.Net"))
                                     ));
            //xdDomain.Element("Domain").Attribute("Id","df");

            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.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("Name").Value, item.book.Element("Name").Value);
            }
        }


Quote

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

This post has been edited by andrewsw: 26 January 2013 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 petkovic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-August 11

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 07:20 AM

View Postandrewsw, on 26 January 2013 - 06:21 AM, said:

Please post the code that constructs the Id attributes.

 XDocument xdDomain = new XDocument(new XElement("Domains",
                  new XElement("Domain",new XAttribute("Id","L001"),"LINQ") ,
                  new XElement("Domain", new XAttribute("Id", "C001"), "C#"),
                  new XElement("Domain",new XAttribute("Id","A001"),"ASP.Net")));
           

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 07:25 AM

Hello. I've answered with my previous post :)
Was This Post Helpful? 0
  • +
  • -

#6 petkovic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-August 11

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 10:04 AM

View Postandrewsw, on 26 January 2013 - 07:25 AM, said:

Hello. I've answered with my previous post :)/>/>

Thank you AndrewsW, you are offered a possible solution, but your XML document structure - xdDomain is different from mine.
In my altered structure of XML-xdDomain , element <Name> does not exist.! I would like a solution provided that the XML structure is just the kind of which I sent in to generate in your reply.

In any case, thanks for the answer !
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 10:28 AM

Erm, were you not able to make the minor change required on your own?

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

Was This Post Helpful? 1
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 12:00 PM

So much easier to write and read in VB.net with its XML-Literals
Spoiler

This post has been edited by AdamSpeight2008: 26 January 2013 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 petkovic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-August 11

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 01:17 PM

View Postandrewsw, on 26 January 2013 - 10:28 AM, said:

Erm, were you not able to make the minor change required on your own?

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

AndrewsW, the problem is now resolved fully from.
I have no more questions!

Thank you very much for your answers.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: LINQ to XML - joining xml data

Posted 26 January 2013 - 01:31 PM

View Postpetkovic, on 26 January 2013 - 01:17 PM, said:

AndrewsW, the problem is now resolved fully from.
I have no more questions!

Thank you very much for your answers.

You are welcome. Andy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1