0

I am trying to generate an XML file using the data from a class, which has a name and multiple addresses associated to the name. I am getting lost @ adding multiple addresses to the XElement. Can somebody please help me. Thanks in advance BB.

My Classes :


     public class Subject
    {
        public ClueName name { get; set; }
        public List driverAddress { get; set;}
    }


 public class DriverAddress
    {
        public string house { get; set; }
        public string street1 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip4 { get; set; }
    } 

private string BuildRequestXML(List <Subject> input)
      {
           string subjectId = "S1" ;
           XElement req = new XElement("order",
                              new XElement("order_dataset",
                                  new XElement("subjects",
                                      from i in input
                                      select 
                                      new XElement("subject", new XAttribute("id", subjectId),
                                          new XElement("name",
                                                        new XElement("first",i.name.first),
                                                        new XElement("middle", i.name.middle ),
                                                        new XElement("last", i.name.last)
                                                       )
                                                    )
                                               ),
                                           new XElement("addresses",
                                                input.Select(c => {c.driverAddress.Select (d =>
                                                    new XElement("address",                                
                                                    new XElement("street1",d.street1),
                                                    new XElement("city",d.city),
                                                    new XElement("state",d.state),
                                                    new XElement("postalcode",d.postalcode)
                                                     )).ToList ();
                                                     }).ToList()
                                                        )
                                           )
                                     );
        }

1 Answer 1

2

I think the problem is in the input.Select(c => {c.driverAddress.Select (d => section.

You are already iterating though input as i, so can do along the lines of

from d in i.driverAddress
select new XElement("address", 
             new XElement("street1", d.Street1),
             new XElement("street2", d.Street2),

etc...

UPDATE: Since driver addresses should not be output as children of the subject, try the following:

var addresses = new List<DriverAddress>();
input.ForEach(delegate(Subject s) { s.driverAddress.ForEach(d => addresses.Add(d)); });


string subjectId = "S1";
XElement req = new XElement("order",
    new XElement("order_dataset",
        new XElement("subjects",
            from i in input
            select
            new XElement("subject", new XAttribute("id", subjectId),
                new XElement("name",
                    new XElement("first", i.name.first),
                    new XElement("middle", i.name.middle),
                    new XElement("last", i.name.last)
                )
            )
        ),
        new XElement("addresses",
                from d in addresses
                select new XElement("address",
                        new XElement("street1", d.street1),
                        new XElement("city", d.city),
                        new XElement("state", d.state),
                        new XElement("postalcode", d.postalcode)
                )
        )

    )
);
Sign up to request clarification or add additional context in comments.

4 Comments

thank you. My requirement is that all the subjects should be under <subjects> tag and all the addresses under <addresses> tag. so, "input.Select(c => {c.driverAddress.Select (d =>" is not at all dependent on "i"
please could you post how you would like the resultant XML to look like?
<subjects> <subject subjectId="1"> <firstName></firstName> <lastName></lastName> </subject> <subject subjectId="2"> <firstName></firstName> <lastName></lastName> </subject> </subjects> <addresses> <address> </address> <address> </address> <address> </address> </addresses>
edited, so that it builds addresses first into an IEnumberable<DriverAddress>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.