[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1427
  • Last Modified:

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
0
KeeterBlack
Asked:
KeeterBlack
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
Please clarify what you mean by "how to use Strongly Typed in the XElements".
0
 
KeeterBlackAuthor Commented:
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
0
 
Bob LearnedCommented:
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/
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
KeeterBlackAuthor Commented:
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
0
 
Bob LearnedCommented:
You should be able to control some aspects of the output XML, such as the element names, with the DataMember attribute.
0
 
Bob LearnedCommented:
Example:

Data Contract Names
http://msdn.microsoft.com/en-us/library/ms731045.aspx

[DataContract]
    public class PurchaseOrder
    {
        // This data member is named "Amount" by default.
        [DataMember]
        public double Amount;

        // The default is overridden to become "Address".
        [DataMember(Name = "Address")]
        public string Ship_to;
    }
    // The namespace is the default value:
    // http://schemas.datacontract.org/2004/07/Contoso.OrderProc
    // The name is "PurchaseOrder" instead of "MyInvoice".
    [DataContract(Name = "PurchaseOrder")]
    public class MyInvoice
    {
        // Code not shown.
    }

    // The contract name is "Payment" instead of "MyPayment" 
    // and the Namespace is "http://schemas.example.com" instead
    // of the default.
    [DataContract(Name = "Payment",
        Namespace = "http://schemas.example.com")]
    public class MyPayment
    {
        // Code not shown.
    }

Open in new window

0
 
KeeterBlackAuthor Commented:
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
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now