0

I have an XML document that I want to modify the connectionstrings to. How do I do a foreach loop and in this example modify the value of LocalSqlServer?

<connectionStrings>
    <clear />
    <add name="Localip" connectionString="Data Source=db01;Initial Catalog=TestA;Integrated Security=True;"
     providerName="System.Data.SqlClient" />
    <add name="LocalSqlServer" connectionString="Data Source=db02;Failover Partner=db01;Initial Catalog=TestB;Integrated Security=True;"
     providerName="System.Data.SqlClient" />
    <add name="ServerAp" connectionString="Data Source=LAPTOP;Initial Catalog=testc;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

This is what I've tried, but I really just want to modify the value not the whole content. For this example I want to change:

<add name="LocalSqlServer" connectionString="Data Source=db02;Failover Partner=db01;Initial Catalog=TestB;Integrated Security=True;"
     providerName="System.Data.SqlClient" />

<add name="LocalSqlServer" connectionString="Data Source=db07;Failover Partner=db07;Initial Catalog=TestB;Integrated Security=True;"
     providerName="System.Data.SqlClient" />

This is what I've tried:

System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
        xmlDocument.Load(@"C:\xml.xml");
        XmlNodeList elemList = xmlDocument.GetElementsByTagName("connectionStrings");
        for (int i = 0; i < elemList.Count; i++)
        {
            foreach (XmlNode chldNode in elemList[i].ChildNodes)
            {
                Console.WriteLine(chldNode.Name.ToString());
                if (chldNode.Name.ToString() == "add")
                {
                    foreach (XmlAttribute xmlAtt in chldNode.Attributes)
                    {
                        if (xmlAtt.Value == "LocalSqlServer")
                        {
                            xmlAtt.InnerXml = "MyNewValue";
                            xmlDocument.Save(@"C:\xml2.xml");
                            break;
                        }
                    }
                }
            }
        }
3
  • connectionStrings is an attribute not an element. add is the element. Commented Nov 6, 2012 at 15:37
  • @L.B this modifies the line i'm looking for, but all i want to modify is the value not the entire string, I know xmlAtt.InnerXMl modifies the whole thing but thats not what I want to do Commented Nov 6, 2012 at 15:38
  • In this case, I think that an XPath query would be far more simple to implement and far more simple to read. Commented Nov 6, 2012 at 15:40

2 Answers 2

1
var xDoc = XDocument.Load(@"C:\xml.xml")
var node = xDoc.XPathSelectElement("//add[@name='LocalSqlServer']");
node.Attribute("connectionString").Value = "some value";

or as SteveB suggested

var node = xDoc.XPathSelectElement("//connectionStrings/add[@name='LocalSqlServer']");
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Using XPath is a lot simpler than looping the nodes. I would use however, the following query: configuration/connectionString/add[@name='LocalSqlServer'] to avoid selecting other unwanted add node in the xml document (add is too generic and limiting by the name is not enough)
0

Using System.Xml.Linq:

var xml = XDocument.Load(fileName);
var localSqlServer = xml.Descendants("connectionStrings").Elements("add").FirstOrDefault(o => o.Attribute("name").Value == "LocalSqlServer");
if (localSqlServer != null)
    localSqlServer.SetAttributeValue("connectionString", "Your New Connection String");
xml.Save(fileName);

3 Comments

it is defined on XDocument class, it is not an extension method
I'm getting Error 3 'System.Xml.XmlDocument' does not contain a definition for 'Descendants' and no extension method 'Descendants' accepting a first argument of type 'System.Xml.XmlDocument' could be found (are you missing a using directive or an assembly reference?)
It is XDocument (defined in System.Xml.Linq), not System.Xml.XmlDocument (defined in System.Xml). XDocument is the new Microsoft recommended way of working with XML in .NET since .NET 3.5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.