Welcome to AspAdvice Sign in | Join | Help

How to edit an XML file that contains a namespace

I must admit using xpath to query and find a specific node in an XML file is not my strongest point and when a namespace was part of the XML file it threw me off even more. A huge thank you goes out to Adam Sills for pointing out my errors.

Take the following XML file that was generated using the June 2005 Patterns and Practices library:

<?xml version="1.0" encoding="utf-8"?>
<dataConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data , Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.databaseSettings xmlns:xsd="
http://www.w3.org/2001/XMLSchema " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultInstance="SQL Server Instance" xmlns=" http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
      <databaseTypes>
        <databaseType name="Oracle" type="Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase , Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />
        <databaseType name="SQL Server" type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase , Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />
      </databaseTypes>
      <instances>
        <instance name="Oracle Instance" type="Oracle" connectionString="Oracle Connection String" />
        <instance name="SQL Server Instance" type="SQL Server" connectionString="SQL Server Connection String" />
      </instances>
      <connectionStrings>
        <connectionString xsi:type="OracleConnectionStringData" name="Oracle Connection String">
          <parameters>
            <parameter name="Data Source" value="null" isSensitive="false" />
            <parameter name="Password" value="null" isSensitive="false" />
            <parameter name="User Id" value="null" isSensitive="false" />
            <parameter name="Host" value="null" isSensitive="false" />
          </parameters>
          <packages />
        </connectionString>
        <connectionString name="SQL Server Connection String">
          <parameters>
            <parameter name="database" value="null" isSensitive="false" />
            <parameter name="Integrated Security" value="True" isSensitive="false" />
            <parameter name="server" value="(local)" isSensitive="false" />
          </parameters>
        </connectionString>
      </connectionStrings>
    </enterpriseLibrary.databaseSettings>
  </xmlSerializerSection>
</dataConfiguration>

Now what if I wanted to edit the Data Source value of the connectionString node that has a value of "Oracle Connection String"? To be brief let us assume I have a command button on a web form. The following code is placed in the Click event procedure:

// Instantiate a XmlDocument Object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(file); //file that is passed in

// Instantiate a XmlNameSpaceManager Object
XmlNamespaceManager xmlNsMgr = new XmlNamespaceManager(xmlDoc.NameTable);

// Add the namespace
xmlNsMgr.AddNamespace("x", "
http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data ");

// Data Source XPath String
string _xpathDS = "//x:connectionStrings/x:connectionString[@name='Oracle Connection String']/x:parameters/x:parameter[@name='Data Source']";

// Instantiate a XmlNode Object
System.Xml.XmlNode node1 = xmlDoc.SelectSingleNode(_xpathDS, xmlNsMgr);
if (node1 != null)
{
 node1.Attributes["value"].Value= "oracledb";
}

xmlDoc.Save(file);

That is all there is to it. As long as node1 is not null then the value of the Data Source is set to oracledb.

Published Sunday, March 26, 2006 10:16 PM by sswafford
Filed under: , ,

Comments

No Comments

New Comments to this post are disabled