Try This:

XDocument xmlDoc = XDocument.Load(CONFIG_FILE);

Dim ndSetting as XmlNodeList
ndSetting = XDocumentxmlDoc.GetElementsByTagName("Setting")
return ndSetting.Count

If you are going to search by the attribute change the
GetElementsByTagName to GetElementsById.  Also if you have a number of
setting elements you can search by using the index ex. ndSetting(0)

Hope this helps,
Dan


On Jul 27, 7:31 pm, rbr <[email protected]> wrote:
> I am working on an app that will, among other things, manage a custom
> XML-based config file. I want to be able to check for the existance of
> a particular element before writing the setting. I have a file like
> the following:
>
> <ConfigurationSettings>
>
>      <Settings>
>           <Setting SettingName="Setting1" />
>      </Settings>
>
> </ConfigurationSettings>
>
> For some reason that I cannot see (likely I am missing something
> obvious) the following code is not succeeding in finding the existing
> "Setting" element in the "Settings" section.
>
>             XDocument xmlDoc = XDocument.Load(CONFIG_FILE);
>
>             IList<XElement> chkForElement = xmlDoc.Descendants
> ("ConfigurationSettings")
>                 .Where(x => x.Elements("Setting").Any())
>                 .ToList();
>
>             return chkForElement.Count > 0;
>
> With the above supplied XML sample, should this not return True? When
> I debug, I see the XML loaded properly in the first line. But, the
> chkForElement.Count is always 0.
>
> Any help with this would be greatly appreciated.
>
> Thanks in advance!
>
> rbr

Reply via email to