在asp.net页面中显示xml

时间:2013-06-11 03:00:05

标签: c# asp.net .net xml

我用XElement创建了xml,但当我在aspx页面上显示时,格式不是我想要的格式:

enter image description here

c#c​​ode:

var persons = new[] {
    new Person {
        Name = "Patrick Hines",
        PhoneNumbers = new[] { "206-555-0144", "425-555-0145" }
    },
    new Person {
        Name = "Gretchen Rivas",
        PhoneNumbers = new[] { "206-555-0163" }
    }
};

XElement contacts = new XElement("contacts",
                        from p in persons
                        select new XElement("contact",
                             new XElement("name", p.Name),
                                 from ph in p.PhoneNumbers
                                 select new XElement("phone", ph)
                              ));

            Response.Write(contacts);
class Person
{
    public string Name;
    public string[] PhoneNumbers;
}

2 个答案:

答案 0 :(得分:2)

如果要返回XML文档,则必须更改内容类型:

  ...
  Response.ContentType = "text/xml"; 
  Response.Write(contacts);

答案 1 :(得分:0)

尝试使用此代码:

Response.Write(contacts.ToString());