In this case you cannot use XPath but you can write some additional code to
make it work using GetElementByTagName. First find all 'add' elements and
then get the attributes you need to modify:
XmlDocument doc = new XmlDocument();
doc.Load("a.xml");
XmlNodeList list = doc.GetElementsByTagName("add");
foreach(XmlElement elmt in list)
{
XmlAttribute attr = elmt.Attributes["key"];
if(attr != null && attr.Value=="db_server")
{
elmt.Attributes["value"].Value = "test";
}
}
doc.Save("b.xml");
The GetElementByTagName will return all 'key' elements in the file (as
opposed to only the ones under configuration/appSettings) so this solution
should work as long as there are no other elements in the xml with the same
name ('key') that serve a different purpose. In case there are then you can
use a differnt approach by manually walking the DOM tree.
"Raj Kumar" <Ra******@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
Hi! Thank you for responding :-)
I am working with C# in .NET compact framework and it doesnot seem to
support doc.SelectNodes. Instead we can use GetElementsByTagName but then
I don't know what should go in there. I tried:
node_list = xmlDoc.GetElementsByTagName("/configuration/appSettings/add
[@key='db_server'] [@value='']");
foreach(XmlAttribute attr in node_list)
{
attr.Value = server_name;
}
xmlDoc.Save(file_path);
This did not give out anything. Any other ideas?
Thanks and regards,
Kumar
"Asad Jawahar" wrote:
The easiest way would be to load the document in XmlDocument and edit
the value of the node. (The following code uses XPath to find the empty
value attributes)
XmlDocument doc = new XmlDocument();
doc.Load("old.xml");
XmlNodeList list =
doc.SelectNodes("/configuration/appSettings/add[@value='']/@value");
foreach(XmlAttribute attr in list)
{
attr.Value = "test";
}
doc.Save("new.xml");
"Raj Kumar" <Ra******@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.com... Hi!
I have a text file that looks like this:
<configuration>
<appSettings>
<add key="db_server" value="someserver" />
<add key="db_database" value="somedatabase" />
</appSettings>
</configuration>
Now, I want to check if value for db_server is empty and if it is, I
want to write the value. I am trying to use XmlTextWriter and XmlTextReader
but being new to XML processing I don't know exactly how to do it.
Can anyone help please?
Regards,
Kumar