tags:

views:

48

answers:

4

Hi everybody

I want to show my data to a asp.net Page using c# in XML format

Plz anybody send some example with code.

like -

<person>
    <email>[email protected]</email>
        <dob>YYYY-MM-DD- HH:MM:SS</dob>
        <city>XYZ</city>
    </email>
</person>

So plz send some code with a good example.

Thanks in Advance

+2  A: 

format your string in Html

then add the values there and

add

Response.ClearHeaders();
Response.AddHeader("content-type", "text/xml");

then write the string to browser

    response.write(yourstring);

example --

        string str = "<root>" + "<person>" + personName + "</person>";
        str += "<details>";
        str += "<DOB>" + "yyyy-MM-dd hh:mm:ss" + "</DOB>";
        str += "<City> " + "XYZ" + "</City>";
        str += "</details>";
        str += "</root>";
        Response.ClearHeaders();
        Response.AddHeader("content-type", "text/xml");
        Response.Write(str);
        Response.End();
Umakanta.Swain
+1  A: 

I advice you to use repeater Control. In this control add table to its ItemTemplate. And after button click Bind The xmlDataSource to this repeater

  <table>
     <ItemTamplate>
      <tr>
          <td colspan="3"><person></td>
      </tr>
      <tr>
        <td colspan="2"><email><%#Bind('email')%></email></td>
      </tr>
      <tr>
       <td></td> <td></td> <td><dob><%#Bind('date')%></dob></td>
      </tr>
      <tr>
        <td></td> <td></td> <td><city><%#Bind('city')%></city></td>
      </tr>
      <tr>
         <td colspan="3"></email></td>
      </tr>
    </person>
    </ItemTamplate>
    </table>
AEMLoviji
It ll not show in xml format in browswer.......
Umakanta.Swain
why? my created Table Template will show it as xml. Why you think that it will not show in xml format in browser?
AEMLoviji
+1  A: 
naval
+2  A: 

I am giving you a generic solution

Create a class **

public class Person
    {
        public string Email { get; set; }
        public string DOB { get; set; }
        public string City { get; set; }
    }

**

After that write this method in your any class library like that

Public Class Utilities
{
    public static XmlElement Serialize(object transformObject)
            {
                XmlElement serializedElement = null;
                try
                {
                    MemoryStream memStream = new MemoryStream();
                    XmlSerializer serializer = new XmlSerializer(transformObject.GetType());
                    serializer.Serialize(memStream, transformObject);
                    memStream.Position = 0;
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(memStream);
                    serializedElement = xmlDoc.DocumentElement;
                }
                catch (Exception SerializeException)
                {

                }
                return serializedElement;
            }

}

Now write this main function at your page , where you want make this task

private void MainMethod()
{
Collection<Person> mPersons = new Collection<Person>();
            //Fill your collection object mPersons with data 
            // I am giving here example for demo
            Person sPerson = new Person();
            sPerson.City = "City 1";
            sPerson.DOB = DateTime.Now.ToString("YYYY-MM-DD HH:MM:SS"); //just for example 
            sPerson.Email = "[email protected]";
            mPersons.Add(sPerson);
            //add another class object
            sPerson = new Person();
            sPerson.City = "City 2";
            sPerson.DOB = DateTime.Now.ToString("YYYY-MM-DD HH:MM:SS"); //just for example 
            sPerson.Email = "[email protected]";
            mPersons.Add(sPerson);

            XmlElement xE = (XmlElement)Utilities.Serialize(mPersons);
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xE.OuterXml.ToString());
            xDoc.Save(Server.MapPath("myFile.xml"));//give your file path name may be in your web application folder   

}

Try this, if you have class object or dataset

AsifQadri