如何从Web服务解析XML数据?

时间:2013-10-25 01:49:02

标签: asp.net asp.net-web-api xml-parsing

我成功使用此代码从App_Data文件夹加载XML。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using Newtonsoft.Json;
using System.Xml.Linq;
using System.Xml;
using XMLTest2.Models;

namespace XMLTest2.Controllers
{
    public class PeopleController : ApiController
    {
        public List<XMLpeople> GetAllPeople()
        {
            var path = HttpContext.Current.Server.MapPath(@"~/App_Data/XML_Test.xml");
            List<XMLpeople> people = new List<XMLpeople>();
            XMLpeople NewPerson = new XMLpeople();
            XDocument doc = XDocument.Load(path);

            foreach (XElement element in doc.Descendants("people").Descendants("person"))
            {
                NewPerson.id = element.Element("id").Value;
                NewPerson.name = element.Element("name").Value;
                NewPerson.age = element.Element("age").Value;
                NewPerson.description = element.Element("description").Value;
                people.Add(NewPerson);
            }

            return people;
        }
    }
}

现在我希望从此为客户创建一个宁静的服务。我试图用这些代码解析这几天。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using System.Xml.Linq;
using System.Xml;

namespace XMLTest2.Models
{
    public class PeopleRestService
    {
        public List<XMLpeople> GetAllPeople()
        {
            string current_hostname = System.Web.HttpContext.Current.Request.Url.ToString();
            string api_url = "api/people";
            string url = current_hostname + api_url;

            WebClient proxy = new WebClient();
            using (var stream = proxy.OpenRead(url))
            {
                var response = proxy.DownloadString(url);
                XmlReader reader = XmlReader.Create(response); //I Got an error here. It says. Illegal characters in path.
                DataTable datatable = new DataTable();
                datatable.ReadXml(reader);

                List<XMLpeople> xxx = new List<XMLpeople>();

                xxx = (from DataRow row in datatable.Rows
                       select new XMLpeople
                       {
                           id = row["id"].ToString(),
                           name = row["name"].ToString(),
                           age = row["age"].ToString(),
                           description = row["description"].ToString()
                       }).ToList();
                return xxx;
        }
    }
 }
}

任何帮助或链接都会非常感激谢谢!,并且为了记录我是Asp.net MVC web api的新手。

1 个答案:

答案 0 :(得分:0)

问题在于XmlReader.Create(string) does not take in an XML string!它接受一个URI - 传递包含XML的字符串肯定会导致异常!

相反,请使用XmlReader.Create(Stream)XmlReader.Create(TextReader)。在这种情况下,使用StringReaderTextReader的子类型)来加载数据非常容易:

var response = proxy.DownloadString(url);
using (var sr = new StringReader(response)) {
  XmlReader reader = XmlReader.Create(sr);
  // ..
}

也可能直接使用stream - 即XmlReader reader = XmlReader.Create(stream) - 并完全跳过DownloadString,尽管他们的可能是一些编码差异。