1

I've read about all I could get on how to read from an XML file, but I can't get anything done. I want to read a connectionstring from an XML file, plain and simple, nothing more.

My XML looks like

<?xml version="1.0" standalone="yes"?>
<connectionString>  
    <conn>"adsf"</conn>
</connectionString>

And I've tried varios way with

XmlDocument doc = new XmlDocument();
XmlTextReader reader = new XmlTextReader(xmlLoc);

while (reader.MoveToNextAttribute())
{
    XmlNode a = doc.ReadNode(reader);
    textBox1.Text = Text + a.Name;
}

XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlLoc); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
XmlNodeList xnList = xml.SelectNodes("/connectionString");

foreach (XmlNode xn in xnList)
{
    XmlNode example = xn.SelectSingleNode("conn");

    if (example != null)
    {
        string na = example["Name"].InnerText;
        string no = example["NO"].InnerText;
    }
}

I'm missing something and I'm not sure what, this should be a very simple task, but I can't get it done. Any help?

I'm trying to do it in a WIndows form application program.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
CiucaS
  • 2,010
  • 5
  • 36
  • 63

4 Answers4

6

I'm missing something?

Yes. .NET has a built in mechanism for storing your connection string, inside an App.config file, there is no reason to manually store it and parse it yourself.

Right click your project, go to Add -> New Item

Then, add a "Application Configuration File":

Application Configuration File

Once it opens, add a connectionStrings node to it, as follows:

// This is an example of a connectionString node. You may add child
// values to it as follows
<connectionStrings>
  <add name="YourConnectionStringKey" 
             providerName="System.Data.SqlClient"
             connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YourDB;Trusted_Connection=Yes" />
</connectionStrings>

Then, you access it using ConfigurationManager.ConnectionStrings:

string connectionString = 
                ConfigurationManager.ConnectionStrings["YourConnectionStringKey"].ConnectionString;
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2

You can use linq to xml.

var xmlStr = File.ReadAllText("fileName.xml");


var str = XElement.Parse(xmlStr);

var result = str.Elements("word").
Where(x => x.Element("connectionString").Value.Equals("conn"));

Console.WriteLine(result);

This might work (or may be some changes you need to make.

v2v2
  • 110
  • 8
0
var x = XElement.Parse(@"<?xml version=""1.0"" standalone=""yes""?><connectionString><conn>adsf</conn></connectionString>");
// or var x = XElement.Load(@"c:\temp\my.xml");
var s = x.Element("conn").Value;
stovroz
  • 6,835
  • 2
  • 48
  • 59
0
If you still have not found your answer then try this (a bit old school but it worked for me)

public class Common
    {
        public Common()
        {
        }
//      public static string GetXML()
//      {
//          return @"C:\MyLocation\Connections.xml";
//      }
        public static string GetXMLconn(string strConn)
        {
            string xmlConStr = "";
            //
            string XMLconn = @"C:\Mylocation\Connections.xml"; 

            // Get the Connection String from the XML file.
            XmlTextReader textReader  = new XmlTextReader(XMLconn);
            textReader.Read();
            while (textReader.Read())
            {
                // Read the currect element in the loop
                textReader.MoveToElement();
                // If the element name is correct then read and assign the connection string
                if (textReader.Name == strConn)
                {
                    xmlConStr = textReader.ReadString();
                }
            }
            textReader.Close();

            return xmlConStr;

        }

}
John
  • 11