来自xml的提取值中的问题 [英] problem in extract values from xml

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

问题描述

亲爱的朋友们

我遇到一个问题就是读取xml文件我的代码是



Dear friends
iam facing a problem to read a xml file my code is

var xmlStr = File.ReadAllText("ConfigurationManager.config");
var messagesElement = XElement.Parse(xmlStr);
var messagesList = (from message in messagesElement.Elements("add")
select new
{
   Connectionstring = message.Attribute("connectionString").Value,
   Provider = message.Attribute("providerName").Value,
   Name = message.Attribute("name").Value
}).ToList();

public class XmlData 
{
   public string Connectionstring { get; set; }
   public string Provider { get; set; }
   public string Name { get; set; }
}





和我的xml文件是



and my xml file is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="DAL.Properties.Settings.OnlineExamConnectionString"

            connectionString="Data Source=S90\SQLEXPRESS;Initial Catalog=OnlineExam;Persist Security Info=True;User ID=sa;Password=123456"

            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration> 





但我无法从XML文件中提取值



but i cannot extract values from my XML file

推荐答案

从配置文件获取连接字符串 [ ^ ]



有一个选项可以按名称检索连接字符串。你不应该解析config,它有所有的类。
Get connection string from config file[^]

There is an option for retrieving the connection string by name. You shouldn't parse config, there are classes for all of it.


我不是LINQ to XML的专家。

但我做了一些谷歌搜索,它似乎你需要使用Descendants而不是Elements。

如何:检索单个属性(LINQ to XML):

http://msdn.microsoft.com/en-gb/library/bb387086.aspx [ ^ ]

I am no expert with LINQ to XML.
But I did some googling and it seems as though you need to use Descendants instead of Elements.
How to: Retrieve a Single Attribute (LINQ to XML):
http://msdn.microsoft.com/en-gb/library/bb387086.aspx[^]
var messagesList = (from message in messagesElement.Descendants("add")
       select new
       {
           Connectionstring = message.Attribute("connectionString").Value,
           Provider = message.Attribute("providerName").Value,
           Name = message.Attribute("name").Value
       }).ToList();



希望有所帮助。


Hope that helps out.


这篇关于来自xml的提取值中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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