tags:

views:

96

answers:

2

What is the modification needed in the following code to get XML from DataContext?

DataClasses1DataContext dc = new DataClasses1DataContext();
var query=new XElement("Numbers",
                                 from p in dc.Pack
                                 select new 
                                           {
                                                   XElement("Number",p.PK),
                                                    XElement("Value",p.Value)
                                            }
                         );
A: 

You're selecting an anonymous type that has two XElement properties. If by "get XML from the DataContext" you mean that you want to construct a valid XElement hierarchy, you need to be selecting two separate XElements.

Try using the Union operator to select multiple objects, rather than selecting an anonymous type with two properties.

DataClasses1DataContext dc = new DataClasses1DataContext();
var query=new XElement("Numbers",
                        (from p in dc.Pack
                         select new XElement("Number",p.PK)
                        ).Union(from p in dc.Pack
                                select new XElement("Value", p2Value))
                       );
womp
A: 

Hey,

would this be a hierarchical load? If so, do:

from p in dc.Pack select new XElement("root", new XElement("Number", p.PK), new XElement("Value", p2Value));

Brian