4

I want to show my data on an ASP.NET page using C# in XML format

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

Do you have any code with examples.

5 Answers 5

8

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Constructing it as a string is a very bad idea because it will be invalid when data isn't escaped properly.
2

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

Comments

1

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>

1 Comment

why? my created Table Template will show it as xml. Why you think that it will not show in xml format in browser?
1

using System; using System.Xml; namespace WriteXmlFile { class Class1 { static void Main(string[] args) { // first you have to create the xml file to any location XmlTextWriter textWriter = new XmlTextWriter("D:\TestxmlFile.xml", null); // to write any things you have to Opens the document textWriter.WriteStartDocument();

// Write first element textWriter.WriteStartElement("Person"); textWriter.WriteStartElement("r", "RECORD", "urn:record"); // Write next element textWriter.WriteStartElement("Email", ""); textWriter.WriteString("DOB"); textWriter.WriteString("City"); textWriter.WriteEndElement(); // WriteChars string[] ch = new string[3]; ch[0] = "[email protected]"; ch[1] = "YYYY-MM-DD"; ch[2] = "xyz"; textWriter.WriteStartElement("Char"); textWriter.WriteChars(ch, 0, ch.Length); textWriter.WriteEndElement(); // Ends the document. textWriter.WriteEndDocument(); // close writer textWriter.Close(); } }

}

Then after that you will find out the desired output

1 Comment

Question was how to output it to the browser, not to save it to the file system.
0

I find it better to use a real XML document object serverside, so that you are sure you are passing a valid XML to the browser.

Something like this :

XmlDocument xml = new XmlDocument();
xml.LoadXml("<xmlcontent />");

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=filename.xml");
xml.Save(Response.OutputStream);

Comments

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.