使用Linq读取XML文件 [英] Reading an XML File With Linq

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

问题描述

我四处张望,但只能找到文件中包含子元素且没有属性的结果!我从我以前的帖子中得到了一些建议,使用Linq来写文件,并且做到了.

I have looked all over but could only find results that had Child Elements in the file and no attributes! I took some advice from a previous post of mine to use Linq to write to the file and I did.

if (System.IO.File.Exists("Guardian.re") == false)
{
    //.re is the file extension that is used

    XDocument doc = new XDocument(
      new XElement("Guardian",
      new XAttribute("IGN",IGN),
      new XAttribute("Hours",hours),
      new XAttribute("Why",WhyRank),
      new XAttribute("Qualifications",Qualify)
       )
     );
}

现在这是我生成的XML

Now here is the XML that I had it generate

<?xml version="1.0" encoding="utf-8"?>
<Guardian>
  <IGN>IGN</IGN>
  <Hours>Hours</Hours>
  <Why>Why</Why>
  <Qualifications>Qualifications</Qualifications>
</Guardian>

现在,我想在列表框中显示这些值,如下所示

Now, I want to display these values in a listbox as follows

Guardian
IGN
Hours
WhyReason
Qualifications

推荐答案

var xdoc = XDocument.Load("Guardian.re"); // load your file
var items = xdoc.Root.Elements().Select(e => (string)e).ToList();

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

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