I want to read the following xml using c#.
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<object></object>
</objects>
</spring>
</configuration>
It wont identify xmlns from objects node.
string xmlFile = @"App1.config";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNodeList nodeList = xmlDoc.SelectNodes("configuration/spring/objects/object");
foreach (XmlElement xmlElement in nodeList)
{
string id = xmlElement.GetAttribute("id"); //Output - SimulatorControlTCP
XmlNodeList xmlPropertyNodeList = xmlElement.SelectNodes("property");
foreach (XmlElement xmlPropertyElement in xmlPropertyNodeList)
{
id = xmlPropertyElement.GetAttribute("value");
if ((id.Contains("tcp") || id.Contains("http")) && id.Contains("localhost"))
{
id = id.Replace("localhost","1.1.1.1");
xmlPropertyElement.Attributes[1].Value = id;
xmlDoc.Save(xmlFile);
}
}
}
It won’t go inside foreach loop. If I remove xmlns , then the above code works fine.
one way:
Next way:
For more about 2nd method see
http://www.codeproject.com/KB/aspnet/ASPNET_20_XMLControl.aspx