Writing XML data to an ASP.NET page

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

<person>
    <email>a@a.com</email>
    <dob>YYYY-MM-DD- HH:MM:SS</dob>
    <city>XYZ</city>
</person>

You have code with examples.

+3
source share
5 answers

format your string in html

then add the values ​​there and

add

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

then write a line in the 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();
+6
source

I give you a general solution

Create 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 any class library like this

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 on your page where you want to perform 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_1@email.com";
            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_2@email.com";
            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 a class object or dataset

+2
source

Control. ItemTemplate. Bind xmlDataSource

  <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

System; System.Xml; namespace WriteXmlFile {    Class1   {       static void Main (string [] args)       {           // xml           XmlTextWriter textWriter = XmlTextWriter ( "D:\TestxmlFile.xml", null);           // , .           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] = "a@a.com"; 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(); } }

}

+1

, XML-, , XML- .

- :

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

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

Source: https://habr.com/ru/post/1767105/


All Articles