1

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

在此处输入图像描述

c#代码:

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;
}
4

2 回答 2

2

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

  ...
  Response.ContentType = "text/xml"; 
  Response.Write(contacts);
于 2013-06-11T04:09:03.073 回答
0

尝试改用这个:

Response.Write(contacts.ToString());
于 2013-06-11T03:28:31.173 回答