c# select single node xpath returns null - c#

I have an app config in a c# project I need to edit at run time. As part of this I have a custom section collection I need to select a single node for. This is the xml and selection statement from my immediate window:
node = xmlDoc.SelectSingleNode("//Customers/add[#id='1']");
null
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="customers" type ="ImporterSupport.CustConfigSection, ImporterSupport"/>
</configSections>
<customers>
<add id ="1" license="gh620g0g0g0g0g3p" ServerAddress ="localhost" ServerPort="8292" SettingsFile ="AutoImportTest.txt" Confirm ="false" DisableRemoveRecips="true" DisableRecoverRecips="true" DisableAlphaId ="true" />
</customers>
</configuration>

Xml is case sensitive. Use
var node = xmlDoc.SelectSingleNode("//customers/add[#id='1']");

Related

Invalid cast exception in System.Configuration.ConfigXmlElement

I am trying to access one of the section of my 'web.config' file using below code
public static string XMLCheck
{
get
{
var section = (Hashtable)ConfigurationManager.GetSection("Default.Framework");
return (string)section["ConnectionString"];
}
}
but getting execption as Unable to cast object of type 'System.Configuration.ConfigXmlElement' to type 'System.Collections.Hashtable' What's wrong here? How to correct
?
Update
<Resources>
<Resource Uri="resource:Default:CrossDomain" Version="1.0" Id="8ae96c54" IsEnabled="True" Handler="handler:Default:Framework:Resources:Data:Oracle">
<Properties>
<Property Name="ConnectionString" Value="Data Source=TESTDB;User Id=TESTUSR;password=TESTPWD;Persist Security Info=false"/>
</Properties>
</Resource>
</Resources>
Verify that the configSections entry in your web.config is a DictionarySectionHandler:
<configuration>
<configSections>
<section name="Default.Framework" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<configuration>
From your updated code, it looks like you're using a library or framework that defines a custom XML structure for its config section. Normally, you would rely on this library to expose the config settings through its properties. If you really want to parse the XML, you could use XPath like the below:
public static string XMLCheck
{
get
{
var section = (XmlElement)ConfigurationManager.GetSection("Default.Framework");
var connectionString = section.SelectSingleNode(#"
descendant::Resource[#Uri='resource:Default:CrossDomain']
/Properties
/Property[#Name='ConnectionString']
/#Value");
return connectionString.Value;
}
}

Reading in a simple XML config file in C#

Im having a hard time remembering how to do this, and most of the examples I'm seeing dont really cover my issue. I'm trying to read in the below XML file, so that if a user selects a Tool Type from a drop-down menu, the variables for said tool will populate a form on screen. I just have no clue how to collect all the elements/attributes for a specific tool.
<?xml version="1.0" encoding="UTF-8"?>
<Tool_menu>
<tool name="A">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>Confg1</name>
</configuration>
<configuration>
<name>Confg2</name>
</configuration>
<configuration>
<name>Confg3</name>
</configuration>
<configuration>
<name>Confg4</name>
</configuration>
</configuration_list>
</tool>
<tool name="B">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>n/a</name>
</configuration>
<configuration>
<name>n/a</name>
</configuration>
</configuration_list>
</tool>
<tool name="C">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>200Scope</name>
</configuration>
<configuration>
<name>300Scope</name>
</configuration>
<configuration>
<name>600Scope</name>
</configuration>
<configuration>
<name>900Scope</name>
</configuration>
</configuration_list>
</tool>
</Tool_menu>
what I'd want is for a user to select 'tool C' and see a list of configurations available on tool C, name of the tool, and a dropdown of options for who to email (low/high severity) that would be tool specific
**using .net 4.5
Access nodes using XPath.
Take a look at some tutorials here or here
In your case, accessing tools C can be achieved like this:
XmlDocument doc = new XmlDocument();
doc.Load(#"c:\temp\tools.xml");
var toolCemails = doc.SelectNodes("//tool[#name='C']/emails/email"); //two nodes with email tag
var toolCconfigs = doc.SelectNodes("//tool[#name='C']/configuration_list/configuration"); //four config nodes
You could take a look at this post on SO: LINQ to read XML
edit: also, in the comments below your question, #zx485 posted a link to another helpful SO post, here: Bind XML data to a Dropdownlist c#
That might help get you started. If not, please try to narrow your question to something more specific - preferably with some code that we can help you with.
As your question is written right now, it seems that you are asking us to do this for you rather than help you with a specific question.

Why no AppSettings in my ConfigurationManager?

I don't know why but i don't have AppSettings in my app.config.file.
If i add it, when i run my apps, appSetting is deleted, so i'm unable to read my data !
Here is my file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</configuration>
And in c# :
string RepertoireEntree = ConfigurationManager.AppSettings["RepertoireEntree"];
You have your app settings in a appSettings element, else it won't be recognized:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
By the way, it is easier to use settings the usual way. You can do this by creating a settings file from your Project Settings > Settings tab.
Visual Studio will generate the XML elements in the app.config for you and you can reference your property like this:
string re = Properties.Settings.Default.RepertoireEntree;
You do not have your xml correct in the app.config file.
note the start and end tags for appSettings
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
PS
While this is probably not an issue for you (but moreso for future readers)
Make sure you have this reference:
Namespace: System.Configuration
Assembly: System.Configuration (in System.Configuration.dll)
as noted on MSDN

ConfigurationSection various names and schema validation

I have custom System.Configuration.ConfigurationSection derived class which represents configuration section in my app.config file. I also have xsd schema document for that XML document. Configuration file has the following (simplified) structure:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sectionOne" type="/*...*/" />
<section name="sectionTwo" type="/*...*/" />
<section name="sectionThree" type="/*...*/" />
</configSections>
<sectionOne xmlns="http://tempuri.org/MySchema.xsd">
<Container>
/*...*/
</Container>
</sectionOne >
<sectionTwo xmlns="http://tempuri.org/MySchema.xsd">
<Container>
/*...*/
</Container>
</sectionTwo >
<sectionThree xmlns="http://tempuri.org/MySchema.xsd">
<Container>
/*...*/
</Container>
</sectionThree >
</configuration>
As we can see, I have a several sections of that type, for various purposes, and I retrieve configuration data using ConfigurationManager class:
ConfigurationManager.GetSection(sectionName);
Because section name is not constant string value, xsd schema validate only elements that are children of the root element (starting from Container tag). Therefore in VS2012, in Error list toolbar, I get a following messages:
Could not find schema information for the element 'http://tempuri.org/MySchema.xsd:SectionOne'.
Could not find schema information for the element 'http://tempuri.org/MySchema.xsd:SectionTwo'.
Could not find schema information for the element 'http://tempuri.org/MySchema.xsd:SectionThree'.
How to fix that validation mechanism.
There is no solution that does not involve changing a XSD: it is not possible to write a schema matching elements with arbitrary names. All the possible valid element names must be specified explicitly, so to validate correctly the names of the section elements must be added either to the http://tempuri.org/MySchema.xsd schema or to DotNetConfig.xsd.

How to read from app.config - LibraryConfigUtilities

How to read values like culture etc. from the app.config file below.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="LibrarySetting" type="LibraryConfigUtilities.ConfigurationSectionHandler, LibraryConfigUtilities" />
</configSections>
<LibrarySetting>
<Country Culture="tr-TR" Currency="TRY" DailyPenaltyFee="5,25" PenaltyAppliesAfter="3">
<WeekendSetting>
<Weekend Day="6"/>
<Weekend Day="0"/>
</WeekendSetting>
<HolidaySetting>
<Holiday Date="25.11.2009"/>
<Holiday Date="26.11.2009"/>
<Holiday Date="27.11.2009"/>
</HolidaySetting>
</Country>
<Country Culture="ar-AE" Currency="AED" DailyPenaltyFee="8.00" PenaltyAppliesAfter="4">
<WeekendSetting>
<Weekend Day="5"/>
<Weekend Day="6"/>
</WeekendSetting>
<HolidaySetting>
<Holiday Date="25.11.2009"/>
<Holiday Date="26.11.2009"/>
<Holiday Date="27.11.2009"/>
</HolidaySetting>
</Country>
</LibrarySetting>
</configuration>
I have below code in my program. And i want to read values from the above app.config.
private List<Country> settingList = new LibrarySetting().LibrarySettingList;
and i added
using LibraryConfigUtilities;
What about using ConfigurationManager.GetSection("LibrarySetting");
See if this will help get you anywhere.
Use this link as a reference, has some good help. App Config Group Help

Categories