从aspx网页读取xml [英] Reading xml from aspx web page

查看:18
本文介绍了从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.

我认为我们可以从 http 响应中读取 xml 文档.这行得通吗?

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

我们如何绑定 XML 与 XSD,以便我们可以将 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

在 C# 中将 XML 反序列化为对象

推荐答案

嗯,基本上,你可以请求一个类似这样的 XML 文档(这里没有 try/catch - 但你绝对应该添加它!):

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 架构所表示的类型.

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天全站免登陆