XML LINQ JOINING 3 ELEMENTS

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The details of my specific case are below. I want to JOIN 3 ELEMENTS from a Microsoft Project XML file. If you can show me the generic syntax that would be great. However I have included specifics below plus a working single join project that works.
I have a Microsoft Project 2010 XML & XSD files
I want to join Resource --< Assignments >--- Tasks and return in memory XML.
To the Microsoft Project XSD file I added the appropriate "key" & "keyref"
<xsd:key name="ResourceUIDKey
<xsd:selector xpath="Resources/Resource/UID"/>
<xsd:field xpath="UID"/>
</xsd:key>
<xsd:keyref name="ResourceUIDKeyRef" refer ="ResourceUIDKey
<xsd:selector xpath="Assignments/Assignment/ResourceUID"/>
<xsd:field xpath="ResourceUID"/>
</xsd:keyref>
<xsd:key name="TaskUIDKey
<xsd:selector xpath="Tasks/Task/UID"/>
<xsd:field xpath="UID"/>
</xsd:key>
<xsd:keyref name="TaskUIDKeyRef" refer ="TaskUIDKey
<xsd:selector xpath="Assignments/Assignment/TaskUID"/>
<xsd:field xpath="TaskUID"/>
</xsd:keyref>
I created a program to JOIN Resources and Assignments but I cant get how to link in the 3rd one which is "Tasks". I want the Task Name.
This is a console project that worksusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;

namespace TestLINQ_xml7
{
class Program
{
static void Main(string[] args)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://schemas.microsoft.com/project/2010", "mspdi_pj14.xsd");
Console.WriteLine("Attempting to validate, ");
XDocument resourceAssignmentDoc = XDocument.Load("project1.xml");

bool errors = false;
resourceAssignmentDoc.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
Console.ReadKey();
errors = true;
});

Console.WriteLine("resourceAssignmentDoc {0}", errors ? "did not validate" : "validated");
Console.ReadKey();

if (!errors)
{
XElement resAss = resourceAssignmentDoc.Element("Project");
XElement newResAss = new XElement("Root",
from r in resAss.Element("Resources").Elements("Resource")
join a in resAss.Element("Assignments").Elements("Assignment")
on (string)r.Element("UID") equals (string)a.Element("ResourceUID")

select new XElement("Assignment",
new XElement("ResourceID", (string)r.Element("UID")),
new XElement("ResourceName", (string)r.Element("Name")),
new XElement("TaskUID", (string)a.Element("TaskUID")),
new XElement("Start", (string)a.Element("Start")),
new XElement("Finish", (string)a.Element("Finish")),
new XElement("TaskName", "XXXXXXX" ))
);
Console.WriteLine(newResAss);
Console.ReadKey();
}

}
}
}
Steven R. Sparks

View the full article
 

Similar threads

P
Replies
0
Views
80
Policy standard local admin account with Active Di
P
S
Replies
0
Views
112
Sudip_inn
S
F
Replies
0
Views
62
FasterThanGravity
F
M
Replies
0
Views
75
Mike_CuttingEdge
M
Back
Top