Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Dear friends
iam facing a problem to read a xml file my code is

C#
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; }
}


and my xml file is
XML
<?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> 


but i cannot extract values from my XML file
Posted
Updated 13-Oct-14 23:27pm
v3
Comments
jing567 14-Oct-14 2:29am    
Can you tell me that at what location exactly u r facing problem? What is ur exact problemm... So that we can provide an alternative code
Sergey Alexandrovich Kryukov 14-Oct-14 2:36am    
What problem?
—SA
srilekhamenon 14-Oct-14 2:41am    
the code is returning 0 elements, no runtime error, Might be iam missing some syntax
Dominic Burford 14-Oct-14 3:52am    
Is there a particular reason why you are using LINQ to determine your connection strings? Why not use the built-in class ConfigurationManager instead? Also, your function reads in the entire contents of the config file to only discard most of it with your LINQ statement. This is inefficient, and for larger files could be costly.
srilekhamenon 14-Oct-14 4:11am    
my requirement is that i want my user to configure his servername, username and password,

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.
 
Share this answer
 
Comments
srilekhamenon 14-Oct-14 5:10am    
Thanks, i was googling from some time but could not found solution
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[^]
C#
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.
 
Share this answer
 
Comments
srilekhamenon 14-Oct-14 5:03am    
Thanks, i was sure iam missing something :)
jaket-cp 14-Oct-14 5:16am    
No problem, glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900