Link to home
Start Free TrialLog in
Avatar of KeeterBlack
KeeterBlackFlag for United States of America

asked on

LINQ to SQL to LINQ XML Strongly Typed XElements

Hi,
  The following code works okay.  What I am looking for is an idea on how to use Strongly Typed in the XElements.  I do have the xsd already set up for the xml output, just cannot figure out how to use them in the XElement lines.  Note I am using Strongly Typed names for the SQL db in the XElements, but having to use strings for the xml part("Name")

Also I am very open on ideas to clean up the code otherwise:

The code is the following:

                  PCCDB_ClassesDataContext db = new PCCDB_ClassesDataContext();
                  ExportDS xds = new ExportDS();

                  XDocument doc = new XDocument();

                  XElement lots =
                        new XElement("Lots",
                             from c in db.LotMasterCurrents
                               where ( c.YARDID == "Pride-01" )
                               select new XElement("Lot",
                                     new XElement("Name",c.YARDID),
                                     new XElement("TotalHeadDays",c.HD_DAYS),
                                     new XElement("InCohorts",
                                     from p in c.Placements
                                             select      new XElement("InCohort",
                                                new XElement("InCount", p.HEAD)
                        )
                        )
                     )
                  );
                  doc.Add( lots );

Thanks,
Keith Black
Avatar of kaufmed
kaufmed
Flag of United States of America image

Please clarify what you mean by "how to use Strongly Typed in the XElements".
Avatar of KeeterBlack

ASKER

The clarification:
 From the above example:

                            new XElement("Name",c.YARDID),

Which already uses a Strongly Typed reference to a data base table-field "c.YARDID" (a VALUE)

What I am asking is there a way I could reference or use a Strongly type field to pull the output XElement name from the out going xsd schemal?  Notice I had to use the string "Name".  Could that parameter or string be replaced with a strongly typed value like xsd.name<- instead of using "Name" which equals a loosely typed field.

I would envision the following

Instead of:
  new XElement("Name",c.YARDID),

Something like:
 new XElement(xsd.Name,c.YARDID,

The reference "Name" is an example of loosely typed field
The reference c.YARDID is an example of a strongly typed value.

Yes I do realize the c.YARDID is pulling a VALUE from the data base. Also I realize the XElement's first parameter needs to be satisfied with a STRING, which I am wanting the STRING to be provided by a Strongly Typed Schema.


Keith
If you need to generate an XML document, then I would look into the DataContractSerializer, which can generate XML from a class marked with the DataContractAttribute.

The DataContractSerializer – XML Files Made Easy
http://softwaredevelopmentforecm.wordpress.com/2009/09/17/the-datacontractserializer-xml-files-made-easy/
Thanks for the input.  I will be looking at the DataContractSerializer.

  The problem that I may have with it is the field names changes from the data base going to the xml.

   Example:
    output field name = "employee" and the input coming from the SQL field name "person"

I will dig deeper though on the DataContractSerializer, sounds like a really neat tool.
Thanks,
Keith
You should be able to control some aspects of the output XML, such as the element names, with the DataMember attribute.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for working on this solution.  I have not had a chance to test your method, but looks like it is what I am  looking for.

Again, Thanks!!

Keith