0

I want to create one XML Format through below My MVC Controller :

 public IHttpActionResult GetCustomerData(CustData model)
    {
        var XMLCustAdd = "<XML><CustAddr></CustAddr></XML>";

        if (model != null)
        {
            XMLCustDtls =   "<XML>" + string.Join("", 
                            "<Customers>" +
                            "<WedAnniv>" + model.WedAnniv.ToString().Trim() + "</WedAnniv>" +
                            "<Locality>" + model.Locality.ToString().Trim() + "</Locality>" +
                            "<HomePhone>" + model.HomePhone.ToString().Trim() + "</HomePhone>" +
                            "<BirthDate>" + model.BirthDate.ToString().Trim() + "</BirthDate>" +
                            "<Town>" + model.Town.ToString().Trim() + "</Town>" +
                            "<Code>" + model.Code.ToString().Trim() + "</Code>" +
                            "<Country>" + model.Country.ToString().Trim() + "</Country>" +
                            "<DtOfCreation>" + model.DtOfCreation.ToString().Trim() + "</DtOfCreation>" +
                            "<Email>" + model.Email.ToString().Trim() + "</Email>" +
                            "<StreetAddr>" + model.StreetAddr.ToString().Trim() + "</StreetAddr>" +
                            "<IsMale>" + model.IsMale.ToString().Trim() + "</IsMale>" +
                            "<MobilePhone>" + model.MobilePhone.ToString().Trim() + "</MobilePhone>" +
                            "<Nm>" + model.Nm.ToString().Trim() + "</Nm>" +
                            "<PostalCd>" + model.PostalCd.ToString().Trim() + "</PostalCd>" +
                            "<State>" + model.State.ToString().Trim() + "</State>" +
                            "<AddressList>" + string.Join("", "<CustAddr>" + model.AddressList.Select(X =>
                                                                                                                             "<Addr1>" + X.Addr1.ToString().Trim() + "</Addr1>" +
                                                                                                                             "<AddressType>" + X.AddressType.ToString().Trim() + "</AddressType>" +
                                                                                                                             "<City>" + X.City.ToString().Trim() + "</City>" +
                                                                                                                             "<MobilePhone>" + X.MobilePhone.ToString().Trim() + "</MobilePhone>" +
                                                                                                                             "<Country>" + X.Country.ToString().Trim() + "</Country>" +
                                                                                                                             "<DateInsert>" + X.DateInsert.ToString().Trim() + "</DateInsert>" +
                                                                                                                             "<Code>" + X.CatCd.ToString().Trim() + "</Code>" +
                                                                                                                             "<Email>" + X.Email.ToString().Trim() + "</Email>" +
                                                                                                                             "<Contact>" + X.Nm.ToString().Trim() + "</Contact>" +
                                                                                                                             "<DefaultAddress>" + X.DefaultAddress.ToString().Trim() + "</DefaultAddress>" +
                                                                                                                             "<Locality>" + X.Locality.ToString().Trim() + "</Locality>" +
                                                                                                                             "<PostalCd>" + X.PostalCd.ToString().Trim() + "</PostalCd>" ) +
                                                              "</CustAddr>"))
                          + "</AddressList>" +
                          "</Customers>"
            + "</XML>";
        }
    }

AND, the XML Format will look like as below :

<XML>
    <Customers>
       <WedAnniv></WedAnniv>
       <Locality></Locality>
       <HomePhone></HomePhone>
       <BirthDate></BirthDate>
       <City></City>
       <Code></Code>
       <Country></Country>
       <DeviceId></DeviceId>
       <DtOfCreation></DtOfCreation>
       <Email></Email>
       <StreetAddr></StreetAddr>
       <IsActive></IsActive>
       <IsGuestUser></IsGuestUser>
       <IsMale></IsMale>
       <LastSession></LastSession>
       <LoyaltyBalance></LoyaltyBalance>
       <MobilePhone></MobilePhone>
       <Nm></Nm>
       <PostalCd>sample string 15</PostalCd>
       <ProfileImage>sample string 9</ProfileImage>
       <State></State>
   <AddressList>
     <CustAddr>
        <Addr1></Addr1>
        <AddressType></AddressType>
        <City></City>
        <MobilePhone></MobilePhone>
        <Country></Country>
        <DateInsert></DateInsert>
        <Code></Code>
        <Email></Email>
        <Contact></Contact>
        <IsBillingAddress></IsBillingAddress>
        <DefaultAddress></DefaultAddress>
        <Lat></Lat>
        <Locality></Locality>
        <Long></Long>
        <PostalCd></PostalCd>
        <State></State>
      </CustAddr> 
  </AddressList>
 </Customers>
  </XML>

AND MY Output is as below :

 <XML>
    <Customers>
         <WedAnniv></WedAnniv> 
         <Locality></Locality> 
        <HomePhone>sample string 16</HomePhone> 
        <BirthDate></BirthDate> 
        <Town></Town> 
        <Code></Code> 
        <Country>sample string 13</Country> 
        <DtOfCreation></DtOfCreation> 
        <Email></Email> 
        <StreetAddr>sample string 9</StreetAddr> 
        <IsMale></IsMale> 
       <MobilePhone>sample string 17</MobilePhone> 
       <Nm></Nm> 
      <PostalCd></PostalCd> 
      <State></State> 
      <AddressList>
   <CustAddr>System.Linq.Enumerable+WhereSelectListIterator`2[System.String]
   </CustAddr> 
   </AddressList>
  </Customers>
</XML>

The AddressList contains object list & my trying top read the data & pass in field in above XML. But it is showing iteration error message & unable to pass the data in AddressList.

Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
  • You forgot to `string.Join` the `model.AddressList.Select`. This `Select` returns a _sequence of strings_, not a single one. – René Vogt Aug 11 '17 at 08:35
  • While using `string.Join` don't concatenate strings passed to it with `+` - do `string.Join("", "" , "", "", model.WedAnniv.ToString().Trim(), "", ...);` – Jakub Dąbek Aug 11 '17 at 08:54
  • 4
    I would *strongly* recommend against building XML like this. Use LINQ to XML - it will lead to simpler and more robust code. (Hint: what does your current code do if someone includes `&` in one of their fields?) – Jon Skeet Aug 11 '17 at 09:08
  • Since you already have a model class why not use a built in XML seriallizer? – user6144226 Aug 11 '17 at 09:09

2 Answers2

0

Try serializing like code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication72
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(XML));
            XML xml = new XML();
            xml.customers = new CustData();
            xml.customers.addressList = new AddressList();
            xml.customers.addressList.custAddr = new CustAddr();

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, xml);
            writer.Flush();
            writer.Close();
        }
    }
    public class XML
    {
        [XmlElement("Customers")]
        public CustData customers { get; set; }
    }
    [XmlRoot("Customers")]
    public class CustData
    {
        public DateTime WedAnniv { get; set; }
        public string Locality { get; set; }
        public string HomePhone { get; set; }
        public DateTime BirthDate { get; set; }
        public string Town { get; set; }
        public string Code { get; set; }
        public string Country { get; set; }
        public DateTime DtOfCreation { get; set; }
        public string Email { get; set; }
        public string StreetAddr { get; set; }
        public Boolean IsMale { get; set; }
        public string MobilePhone { get; set; }
        public string Nm { get; set; }
        public string PostalCd { get; set; }
        public string State { get; set; }
        [XmlElement("AddressList")]
        public AddressList addressList { get; set; }
    }
    [XmlRoot("AddressList")]
    public class AddressList
    {
        [XmlElement("CustAddr")]
        public CustAddr custAddr { get; set; }
    }
    [XmlRoot("CustAddr")]
    public class CustAddr
    {
        public string Addr1  { get; set; }
        public string AddressType { get; set; }
        public string City  { get; set; }
        public string MobilePhone  { get; set; }
        public string Country  { get; set; }
        public DateTime DateInsert { get; set; }
        public string Code { get; set; }
        public string Email { get; set; }
        public string Contact { get; set; }
        public Boolean IsBillingAddress  { get; set; }
        public string DefaultAddress { get; set; }
        public double Lat { get; set; }
        public string Locality { get; set; }
        public double Long { get; set; }
        public string PostalCd { get; set; }
        public string State { get; set; }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
-3

Check this link. It covers more possible scenarios. Linq is not immmediately executed at all times, and much of it is executed when any of next lines of code needs actual output.

http://stackoverflow.com/questions/217805/using-linq-to-concatenate-strings

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • Deferred execution has nothing to do with this. – René Vogt Aug 11 '17 at 08:57
  • Why deferred execution has nothing to do with this? That looks like an iterator to me. Iterator itself has no data. It gets data when it is converted to list or array. Thereafter, it can be string.Join 'ed. – Amit Kumar Singh Aug 11 '17 at 09:07
  • 1
    The error is (despite there being a lot better ways to write xml) that OP is inserting the *iterator* itself into the string instead of the elements of the iterator. – René Vogt Aug 11 '17 at 09:14
  • That's what i said. That's why it is deferred execution. That's again what you said, that he needs to string.join it. He will be able to string.join list or array, a conversion to that will convert iterator to data. – Amit Kumar Singh Aug 11 '17 at 09:16
  • 1
    "Deferred execution" refers to the fact that the iterator might be fetching the results only when you iterate. And that has nothing to with this error here, as the error would also occure with an array (that does not use deferred execution). – René Vogt Aug 11 '17 at 09:19
  • Error will not be same with array which is immediate execution. I agree that he needed string.join also but what he was doing was concatenating string to output of deferred execution which was not there. – Amit Kumar Singh Aug 11 '17 at 09:23