Read and write to template file. (XmlElement)node.SelectSingleNode XPath element returns NULL all th

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have FLD file with xml format as follows:
<div style="color:Black;background-color:White; <pre>
<pre><OpenSearchDescription xmlns=<span style="color:#A31515; "http://a9.com/-/spec/opensearch/1.1/">
<ShortName>My Search</ShortName>
<InternalName xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">MySearch</InternalName>
<Description>Returns results <span style="color:Blue; from MySearch.com</Description>
<Language></Language>
<LocationType xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">OpenSearch</LocationType>
<Version xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">1.0.0.0</Version>
<IsPrefixPattern xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">False</IsPrefixPattern>
<ConnectionUrlTemplate xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">http:<span style="color:Green; //Mysearch/Search.MyFeed.svc/?search={searchTerms}&amp;count=30&amp;t=DNlA0LSWIVo%3d&amp;output=rss&amp;agent=SPPS&amp;pageurltemplate=http://MySearch.com/MyFolder/Pages</ConnectionUrlTemplate>
<MoreLinkTemplate xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location"></MoreLinkTemplate>
<CreationDate xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">4/29/2010 10:50:20 PM</CreationDate>
<LastModifiedDate xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">4/29/2010 10:50:20 PM</LastModifiedDate>
<ExpirationDate xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">1/1/0001 12:00:00 AM</ExpirationDate>
<Visualization name=<span style="color:#A31515; "summary" xmlns=<span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location">
<OpenSearchDescription>
[/code]
[/code]
<div style="color:Black;background-color:White; <pre>
I have to replace node ConnectionUrlTemplate value
[/code]
i.e.
<div style="color:Black;background-color:White; <pre>
http:<span style="color:Green; //Mysearch/Search.MyFeed.svc/?search={searchTerms}&amp;count=30&amp;t=DNlA0LSWIVo%3d&amp;output=rss&amp;agent=SPPS&amp;pageurltemplate=http://MySearch.com/MyFolder/Pages


[/code]
to be replaced with new values i want to set programatically
<pre>http://Mysearch.mysite.com/Search.MyFeed.svc/?search={searchTerms}&amp;count=30&amp;t=DNlA0LSWIVo%3d&amp;output=rss&amp;agent=SPPS&amp;pageurltemplate=http://MySearch.mysite.com/MyNewFolder/Pages[/code]
Actually we have a sub domain so i need to setup this configuration everywhere. I tried following code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{

<span style="color:Blue; string subDomain = <span style="color:#A31515; "http://subDomain.MynewDomain.com/Search/";
WriteSetting(<span style="color:#A31515; "\WebConfigFiles\MyXMLFile.FLD", <span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location", subDomain);


}

<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void WriteSetting(<span style="color:Blue; string SourceFile, <span style="color:Blue; string key, <span style="color:Blue; string value)
{
<span style="color:Blue; string appPath = System.IO.Directory.GetCurrentDirectory() + SourceFile;
XmlDocument doc = loadConfigDocument(appPath);

XmlNamespaceManager nsmRequest = <span style="color:Blue; new XmlNamespaceManager(doc.NameTable);
<span style="color:Green; //nsmRequest.AddNamespace("ns1", "http://a9.com/-/spec/opensearch/1.1/");
<span style="color:Green; //XmlNode node = doc.SelectSingleNode("//ns1:OpenSearchDescription", nsmRequest);

nsmRequest.AddNamespace(<span style="color:#A31515; "ns1", <span style="color:#A31515; "http://schemas.microsoft.com/Search/2007/location");
XmlNode node = doc.SelectSingleNode(<span style="color:#A31515; "//ns1:ConnectionUrlTemplate", nsmRequest);
XmlNode NewNode = doc.SelectSingleNode(<span style="color:#A31515; "//ns1:ConnectionUrlTemplate", nsmRequest);

<span style="color:Blue; if (node == <span style="color:Blue; null)
{
<span style="color:Blue; throw <span style="color:Blue; new InvalidOperationException(<span style="color:#A31515; "section not found in config file.");
}
<span style="color:Blue; try
{
XmlElement elem = (XmlElement)node.SelectSingleNode(<span style="color:Blue; string.Format(<span style="color:#A31515; "//ConnectionUrlTemplate[@xmlns={0}]", key));
<span style="color:Blue; if (elem != <span style="color:Blue; null)
{
<span style="color:Green; // add value for key
elem.SetAttribute(<span style="color:#A31515; "value", value);
}
<span style="color:Blue; else
{
<span style="color:Green; // key was not found so create the add element
<span style="color:Green; // and set its key/value attributes
elem = doc.CreateElement(<span style="color:#A31515; "add");
elem.SetAttribute(<span style="color:#A31515; "key", key);
elem.SetAttribute(<span style="color:#A31515; "value", value);
node.AppendChild(elem);
}
doc.Save(appPath);
[/code]
the elem is returning NULL all the time.
Please help how can i replace my desired change in specific node.
<hr class="sig Shamshad Ali

View the full article
 
Back
Top