当前位置:网站首页>C# 读取web上的xml
C# 读取web上的xml
2022-06-25 06:39:00 【※※冰馨※※】
一、使用LINQ读取
使用Xdocument上的Load方法,可以快速的加载一个XML文档,然后使用LINQ对 加载XML文档进行查询或其他操作,这里仅简单偏历。所以,一旦查询一组元素有返回元素集,就可以使用一个简单的foreach循环访问每一个元素。核心代码如下:
/// <summary>
/// 使用LINQ读取web上的xml
/// </summary>
public static void UseLINQ()
{
string sURL = "http://localhost:9058/GameServerInfo/XMLFile.xml";
XDocument oXDoc = XDocument.Load(sURL);
var qurey = from e in oXDoc.Descendants()
where e.NodeType == XmlNodeType.Element
select new
{
ElementName=e.Name.ToString(),
ElementValue=e.Value
};
foreach(var elementInfo in qurey)
{
HttpContext.Current.Response.Write(string.Format("ElementName->{0} ElementValue->{1}<br />", elementInfo.ElementName, elementInfo.ElementValue));
}
}二、使用XmlReader构造函数
当然还可以通过使用带有URL的XmlReader.Create方法来完成操作,它使用的是一个XmlUrlResolver类的一个实例检测传入的URL,然后打开一个通向由该URL代表的XML文档流。为了指定阅读器上的设置,可以使用另一个Create的重载,它也通过一个XmlReaderSetting实例完成这一目的。代码如下:
/// <summary>
/// 使用XmlReader构造函数
/// </summary>
public static void UseXmlReader()
{
string sURL = "http://localhost:9058/GameServerInfo/XMLFile.xml";
using(XmlReader read=XmlReader.Create(sURL))
{
while(read.Read())
{
switch (read.NodeType)
{
case XmlNodeType.Element:
HttpContext.Current.Response.Write(string.Format("ElementName->{0} <br />", read.Name));
break;
case XmlNodeType.Text:
HttpContext.Current.Response.Write(string.Format("ElementValue->{0}<br />", read.Value));
break;
case XmlNodeType.CDATA:
HttpContext.Current.Response.Write(string.Format("ElementValue->{0}<br />", read.Value));
break;
//other
}
}
}
}边栏推荐
- Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
- TEMPEST HDMI泄漏接收 1
- 【UVM入門 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常規方法、寄存器模型的應用場景
- Harmony美食菜单界面
- Static bit rate (CBR) and dynamic bit rate (VBR)
- [batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
- Can I open a stock account with a compass? Is it safe?
- Authentique Photoshop 2022 expérience d'achat partage
- Pit encountered by pytorch: why can't l1loss decrease during model training?
- 图扑软件数字孪生 3D 风电场,智慧风电之海上风电
猜你喜欢

ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性

Estimation of dense forest volume based on LIDAR point cloud with few ground points

FairMOT yolov5s转onnx

OAuth 2.0一键登录那些事

What if there is no point in data visualization?

【LeetCode】two num·两数之和

STL教程4-输入输出流和对象序列化

Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing

China Mobile MCU product information

PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
随机推荐
指南针可以开股票账户吗?安全吗?
VectorDraw Web Library 10.10
关于硬件问题造成的MCU死机,过来人简单的谈一谈
LabVIEW jump to web page
RTKLIB-b33版本中GALILEO广播星历存储问题
Let's talk about MCU crash caused by hardware problems
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
Collection of common terms and meanings in forestry investigation based on lidar
【UVM入門 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常規方法、寄存器模型的應用場景
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
鸿蒙页面菜单的选择
Construction of occupancy grid map
C Getting Started tutorial
Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
用动图讲解分布式 Raft
Getting started with OpenMP
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
Authentique Photoshop 2022 expérience d'achat partage
Distributed quorum NWR of the alchemy furnace of the Supreme Master