在WebService方法中反回XML对象

aspx页面


	
		
	

	









ReturnXmlService.asmx页面代码

<%@ WebService Language="C#" Class="ReturnXmlService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ReturnXmlService  : System.Web.Services.WebService
{	
    [WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public XmlNode GetXmlDocument()
	{
		XmlDocument doc = new XmlDocument();
		doc.LoadXml("Jeffrey Zhao1000");
		return doc;
    }

	[WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
	public XmlNode GetXmlElement()
	{
		XmlDocument doc = new XmlDocument();
		doc.LoadXml("Jeffrey Zhao1000");

		return doc.DocumentElement;
	}

	[WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
	public Employee GetEmployee()
	{
		return new Employee("Jeffrey Zhao", 1000);
	}

	[WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
	public string GetXmlString()
	{
		return "Jeffrey Zhao1000";
	}

	[WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)]
	public string GetSerializedString()
	{
		return "Jeffrey Zhao1000";
	}
	
}

 

Employee.cs类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;

/// 
/// Summary description for Employee
/// 
public class Employee
{
	public Employee(string name, int salary)
	{
		this.Name = name;
		this.Salary = salary;
	}

	public Employee() { }

	[XmlAttribute]
	public string Name;
	
	[XmlIgnore]
	public int Salary;
}



 

你可能感兴趣的:((C#)AJAX)