[英]writing XML data to an ASP.NET page
I want to show my data on an ASP.NET page using C# in XML format 我想使用XML格式的C#在ASP.NET页面上显示我的数据
<person>
<email>a@a.com</email>
<dob>YYYY-MM-DD- HH:MM:SS</dob>
<city>XYZ</city>
</person>
Do you have any code with examples. 你有任何带有例子的代码吗?
format your string in Html 在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();
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_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 class object or dataset 如果您有类对象或数据集,请尝试此操作
I advice you to use repeater Control. 我建议你使用转发器控制。 In this control add table to its ItemTemplate. 在此控件中将表添加到其ItemTemplate中。 And after button click Bind The xmlDataSource to this repeater 然后在按钮后单击将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>
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] = "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(); } }
} }
Then after that you will find out the desired output 然后,您将找到所需的输出
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. 我发现使用真正的XML文档对象服务器端更好,因此您确定要将有效的XML传递给浏览器。
Something like this : 像这样的东西:
XmlDocument xml = new XmlDocument();
xml.LoadXml("<xmlcontent />");
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=filename.xml");
xml.Save(Response.OutputStream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.