如何读取XML文件中的数据 [英] how to read data from xml file

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

问题描述

<?xml version="1.0" encoding="utf-8" ?> 
<user>
  <username>prince</username> 
  <password>user1</password> 
</user>

这是我的XML文件名作为user.xml。

This is my xml file name as user.xml.

现在,当我在页面上点击按钮,我需要从文件中获取数据,并将这些数据用在像变量:

Now when i click the button in the page, i need to get the data from that file and place that data in variable like:

string strusername =  data  cmg from  xml file (prince)
string strPassword =  data  cmg from  xml file (password)

谁能告诉我如何与语法做到这一点?

Can anyone tell me how to do this with syntax?

感谢您

推荐答案

LINQ到XML 是做你想要什么样的现代生活方式。

LINQ to XML is the modern way to do what you want.

 XDocument xDoc = XDocument.Load("user.xml");
 string strusername =  xDoc.Descendants(XName.Get("username")).First().Value;
 string strPassword = xDoc.Descendants(XName.Get("password")).First().Value;

这篇关于如何读取XML文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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