locked
Generating the XMl File and Store it in the Hosted Sever RRS feed

  • Question

  • User1997618745 posted

    [Route("todo/Get/{country}/{year}")]

    public HttpResponseMessage Get(string country, int year)

    {

    todoactivities todoactivitiesdata = new todoactivities();

    Repository repository = new Repository ();

    HttpResponseMessage message = Request.CreateResponse(HttpStatusCode.OK, repository.GetAll(country, year));

    return message;

    }

    The above action returns the XML as expected, my requirement here is I wanted to save the xml result as a xml file.

    How can I do that? Can you please help me?

    Regards,

    Selva

    Thursday, February 1, 2018 1:25 PM

Answers

  • User1120430333 posted

    If you have a string variable, then you can load it into a XMLDocument(), and save it to a XML file.

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(str);
    doc.Save("Selva.xml"); 

    However, the ASP.NET Worker Process that is servicing that App Pool that is hosting the WebAPI must have permissions to write to the folder to save the file.    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 2, 2018 4:09 AM

All replies

  • User1120430333 posted

    If you have a string variable, then you can load it into a XMLDocument(), and save it to a XML file.

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(str);
    doc.Save("Selva.xml"); 

    However, the ASP.NET Worker Process that is servicing that App Pool that is hosting the WebAPI must have permissions to write to the folder to save the file.    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 2, 2018 4:09 AM
  • User283571144 posted

    Hi Selvakumar Rathinam,

    According to your description, I suggest you could try to use XML serialization to achieve your requirement.

    According to your codes, I guess this line(repository.GetAll(country, year)) will return a list of the value.

    Since I don't have your result type codes, I created a test demo with custom class.I suggest you could change the codes as below to serialize the result.

            static void Main(string[] args)
            {
        
                var customerlist = GetCustomerList();
    
                XmlSerializer serializer = new XmlSerializer(typeof(List<Customer>));
                using (TextWriter writer = new StreamWriter(@"D:\test.xml"))
                {
                    serializer.Serialize(writer, customerlist);
                }
            }
            public static List<Customer> GetCustomerList()
            {
    
                List<Customer> customers = new List<Customer>();
                customers.Add(new Customer { CustomerId = 1, CustomerName = "Tejas Trivedi", PhoneNumber = "23244556", Email = "tejas@gmail.com" });
                customers.Add(new Customer { CustomerId = 2, CustomerName = "Jignesh Trivedi", PhoneNumber = "4545453322", Email = "jignesh@gmail.com" });
                customers.Add(new Customer { CustomerId = 3, CustomerName = "Rakesh Trivedi", PhoneNumber = "333222111", Email = "rakesh@gmail.com" });
                customers.Add(new Customer { CustomerId = 4, CustomerName = "Keyur Joshi", PhoneNumber = "999888822", Email = "keyur@gmail.com" });
                customers.Add(new Customer { CustomerId = 5, CustomerName = "Sachin shah", PhoneNumber = "38888232", Email = "sachin@gmail.com" });
                customers.Add(new Customer { CustomerId = 6, CustomerName = "Mandar Bhatt", PhoneNumber = "343412212", Email = "mandar@gmail.com" });
                return customers;
            }
    
            [Serializable]
            public class Customer
            {
                public int CustomerId { get; set; }
                public string CustomerName { get; set; }
                public string PhoneNumber { get; set; }
                public string Email { get; set; }
            }
    

    Result:

    Best Regards,

    Brando

    Thursday, February 8, 2018 6:51 AM