从ASPX页面读取XML [英] Reading xml from aspx web page

查看:416
本文介绍了从ASPX页面读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须从一个aspx页面读取数据。当我们调用了查询字符串的页面,它返回数据的XML文档的查询字符串匹配。

We have to read data from a aspx page. When we call the page with a query string, it returns an xml document with data that matches the query string.

我们有我们取回XML相匹配的XSD。

We have an XSD that matches the xml that we get back.

我想,我们可以读取XML文档出来的HTTP响应。将这项工作?

I am thinking that we can read the xml document out of the http response. Will this work?

我们如何能够绑定的与XSD的XML,这样我们可以把XML文档就好像它是强类型?

How can we bind the XML with the XSD, such that we can treat the XML document as if it were strongly typed?

谢谢,

设拉子

更新:

上找到如何反序列化这个链接

Found this link on how to deserialize

反序列化XML在C#对象

推荐答案

好了,基本上,你可以要求一个XML文档,像这样(没有尝试/在这里捕获 - 但你一定要补充一点!):

Well, basically, you can request an XML document something like this (no try/catch here - but you should definitely add that!):

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";  // or GET - depends 

myRequest.ContentType = "text/xml; encoding=utf-8";
myRequest.ContentLength = data.Length;

using (Stream reqStream = myRequest.GetRequestStream())
{
  // Send the data.
  reqStream.Write(data, 0, data.Length);
  reqStream.Close();
}

// Get Response
WebResponse myResponse;

myResponse = myRequest.GetResponse();
XmlDocument _xmlDoc = new XmlDocument();

using (Stream responseStream = myResponse.GetResponseStream())
{
   _xmlDoc.Load(responseStream);
}

无论你是否有一个GET或POST取决于您的方案 - 在一个GET,你不会有请求数据走出去

Whether you have a GET or POST depends on your scenario - in a GET, you won't have request data going out.

一旦你有你的XML回为一个XmlDocument的,你可以验证对XML架构,或者只是尝试将其反序列化到被重新被你拥有XSD架构psented $ P $的类型。

Once you have your XML back as a XmlDocument, you can either validate that against the XML schema, or just try to deserialize it into the type that is represented by the XSD schema you have.

如果说工程 - >你有XML是有效的和确定。如果没有,你会得到反序列化异常。

If that works --> the XML you got is valid and OK. If not, you'll get an exception on deserialization.

马克

这篇关于从ASPX页面读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆