Question : problem with generating XML using LINQ and C#

I am trying to generate an XML file using LINQ with C# in a ASP.NET application

The data source is a SQLDataReader which extracts data d=from database...

The problem is that the XML that i'm trying to generate is not in the format I'm after...

The format I want my XML to be in is attached in the file "CorrectFormat.xml"

However i am getting the XML in a slightly different format (attached in the file "WrongFormat.xml")

The code i''m using to generate the xml is in the code section

The data stored in the SQLdatareader returned from the database is as follows

TYPE        ID     VALUE
=====   ===   =========
trial              1      Lung Cancer
diseases      1      Diabetes
diseases      3      Osteoporosis
diseases      5      Cancer
ethnicity      1      White
ethnicity      2      Mixed race
ethnicity      3      Indian


Please help me convert the data into XML in the correct format

Thanks in Advance!!!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
var criteria = da.getTrialCriteria(trialID);
                try
                {
                    var xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
                    var xroot = new XElement("trials");
                    xdoc.Add(xroot);
                    while (criteria.Read())
                    {
                        #region switch statement

                        switch ((string)criteria["type"])
                        {
                            case "trial":
                                {
                                    var xElement = new XElement("trial",
                                                                new XAttribute("ID", criteria[1]),
                                                                new XElement("name", criteria[2])
                                        );
                                    xroot.Add(xElement);
                                    break;
                                }
                            case "diseases":
                                {
                                    var xsubroot = new XElement("diseases");
                                    xdoc.Add(xsubroot);
                                    var xElement = new XElement("disease",
                                                                new XAttribute("ID", criteria[1]),
                                                                new XElement("name", criteria[2])
                                        );
                                    xroot.Add(xElement);
                                    break;
                                }
                            case "ethnicity":
                                {
                                    var xsubroot = new XElement("ethnicities");
                                    xdoc.Add(xsubroot);
                                    var xElement = new XElement("ethnicity",
                                                                new XAttribute("ID", criteria[1]),
                                                                new XElement("name", criteria[2])
                                        );
                                    xroot.Add(xElement);
                                    break;
                                }
                            case "bloodGroup":
                                {
                                    var xsubroot = new XElement("bloodGroups");
                                    xdoc.Add(xsubroot);
                                    var xElement = new XElement("bloodGroup",
                                                                new XAttribute("ID", criteria[1]),
                                                                new XElement("name", criteria[2])
                                        );
                                    xroot.Add(xElement);
                                    break;
                                }
                        }

                        #endregion
                    }
                    xdoc.Save(@"c:/WrongFormat.xml");
                }
Attachments:
 
This is the correct format of the XML I want it in
 
 
This is the wrong format of XML I get
 

Answer : problem with generating XML using LINQ and C#

Hi 2ooth;

I found that this, .Cast<DbDataRecord>(), was causing records not to be returned. So I just read the data reader directly and filled a collection of Data. This should work for you.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
private List<Data> criterias = new List<Data>();

private void button1_Click(object sender, EventArgs e)
{
    // Get the reader from the getTrialCriteria function
    SqlDataReader dr = getTrialCriteria(trialID);
    // Read the data from the reader and create a List<Data> object for each row
    while (dr.Read())
    {
        // The index of the dr must match the column returned from the data reader 
        // as well as the Get datatype must match the returned type
        Data data = new Data() { TYPE = dr.GetString(0), ID = dr.GetString(1), name = dr.GetString(2) };
        // Add the new Data object to the collection
        criterias.Add(data);
    }

    var query = (from c in criterias
                 group c by c.City into groupCity
                 select groupCity).ToList();

    // Create the XDocument and root element
    var xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
    var xroot = new XElement("trials");
    xdoc.Add(xroot);
    
    // Create the trials element and its child
    data trial = query.Find(t => t.Key.ToUpper() == "TRIAL").FirstOrDefault();
    XElement xTrial = new XElement(trial.TYPE, 
        new XAttribute("ID", trial.ID),
        new XElement("name", trial.name)
        );                
    
    // Create the diseases element and all its childrens  
    XElement disease = new XElement("diseases");
    foreach (data dis in query.Find(d => d.Key.ToUpper() == "DISEASES"))
    {
        XElement d = new XElement("disease",
            new XAttribute("ID", dis.ID),
            new XElement("name", dis.name));
        disease.Add(d);
    }
    // Add the disease to the xTrial
    xTrial.Add(disease);
    
    // Create the ethnicities element and all its childrens  
    XElement ethnicities = new XElement("ethnicities");
    foreach (data ethnicity in query.Find(d => d.Key.ToUpper() == "ETHNICITY"))
    {
        XElement eth = new XElement("ethnicity",
            new XAttribute("ID", ethnicity.ID),
            new XElement("name", ethnicity.name));
        ethnicities.Add(eth);
    }
    // Add the ethnicities to the trial
    xTrial.Add(ethnicities);
    // Add everything to the root
    xroot.Add(xTrial);
    
    xdoc.Save(@"CorrectFormat.xml");
}
Random Solutions  
 
programming4us programming4us