title: Can PageMethods return XML?
body: <p>I was playing with some ASP.NET PageMethods, and I tried to simply return XML using this:</p>
  
  <pre><code>[WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
  public static string GetIPLocation(string ip)
  {
      var doc = XDocument.Load(HttpContext.Current.Server.MapPath("relative xml path"));
      var s = doc.ToString();
  
      return s;
  }
  </code></pre>
  
  <p>The exact same code <strong>actually works with ASMX services</strong>, (removing the <code>static</code> modifier though)</p>
  
  <p>But it turns out that I cannot get the XML when using PageMethods, <strong>instead, I'm receiving my page's HTML code.</strong></p>
  
  <p>Here is my AJAX code:</p>
  
  <pre><code>$.ajax({
      url: "&lt;%: this.ResolveClientUrl("~/relative path/GetIPLocation") %&gt;",
      type: "POST",
      contentType: "application/xml; charset=utf-8;",
      dataType: "xml",
      data: "{ip:'"+$target.val()+"'}",
      async: true,
      cache: false,
      success: function (msg) {
          console.log("MSG: %o", msg);
          viewModel.ip1(msg.d.IP);
      },
      error: function (XHResponse, errorMessage, errorCode) {
          console.log("AJAX Error: %o", XHResponse);
      }
  });
  </code></pre>
  
  <p>If I change the response to JSON or if I use an ASMX/WCF service instead of my PageMethod, it works like a charm</p>
  
  <p>So are PageMethods incapable of returning XML by design??... If not what am I missing?</p>
  
  <h3>Edit 1</h3>
  
  <p>As commented by @JamieSee, I made this modification, still the same result:</p>
  
  <pre><code>[WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
  public static XDocument(GetIPLocation(string ip)
  {
       return XDocument.Load(HttpContext.Current.Server.MapPath("relative xml path"));
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>[WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
  public static XmlDocument(GetIPLocation(string ip)
  {
      var x = new XmlDocument();
  
      x.LoadXml(HttpContext.Current.Server.MapPath("relative xml path"));
  
      return x;
  }
  </code></pre>