Linq到XML数据获取 [英] Linq to XML data fetching

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

问题描述

我写了代码



一个xml文件



 <?  xml    版本  =  1.0   编码  =  utf -8  >  
< 兴趣 >
< 兴趣 >
< ID > 1 < / ID >
< 名称 > dance < / Name >
< /兴趣 >
< 兴趣 >
< ID > 2 < / ID >
< 名称 > 娱乐< /名称 >
< /兴趣 >
< 兴趣 >
< ID > 3 < / ID >
< 名称 > Tours < / Name >
< /兴趣 >
< /兴趣 >







i传递id并希望获得名称



  string  strBaseDirectory = AppDomain.CurrentDomain.BaseDirectory; 
string strFullPath = strBaseDirectory + 兴趣。 xml的;

string strid = Request.QueryString [ 挂断];
XElement xe = XElement.Load(strFullPath);
var name = 来自名称 xe.Elements( 兴趣
where string )Name.Element( ID)。值== strid
选择 Name.Value;
Label1.Text = name.Last()。ToString();





但是我得到了结果



1dance



但我只需要跳舞。



我的尝试:



linq to xml:



在标签中显示值。

解决方案

给你的变量更好的名字可能会有所帮助! :)

  var  name = 来自兴趣 in  xe.Elements(  Interest
其中 string )interest.Element( ID)== strid
选择 string )interest.Element( 名称);


i have written the code

one xml file

<?xml version="1.0" encoding="utf-8"?>
<Interests>
  <Interest>
    <ID>1</ID>
    <Name>dance</Name>
  </Interest>
  <Interest>
    <ID>2</ID>
    <Name>Entertainment</Name>
  </Interest>
  <Interest>
    <ID>3</ID>
    <Name>Tours</Name>
  </Interest>
</Interests>




i pass the id and want to get the name

string strBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
              string strFullPath = strBaseDirectory + "Interest.xml";

              string strid = Request.QueryString["Drop"];
              XElement xe = XElement.Load(strFullPath);
              var name = from Name in xe.Elements("Interest")
                         where (string)Name.Element("ID").Value == strid
                         select Name.Value;
              Label1.Text = name.Last().ToString();



but i got the result

1dance

but i need only "dance".

What I have tried:

linq to xml:

show value in a label.

解决方案

Giving your variables better names might help! :)

var name = from interest in xe.Elements("Interest")
    where (string)interest.Element("ID") == strid
    select (string)interest.Element("Name");


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

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