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.
Related
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']");
I have read a lot of post on being able to read from a web.config file. I am trying to relate what I have found to a configuration.config file and have not had much success. Below is the layout of my configuration file.
<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SessionTimeout>20</SessionTimeout>
<ApplicationDirectory>C:\MyCompany\RunningFolder</ApplicationDirectory>
<Rounding>0.5</Rounding>
</Configuration>
I want to be able to do is pull the value but have not found a way to do this. Any assistance is greatly appreciated.
It sounds like you would benefit from using appsettings in your config file. Below is a link to msnd that explains them and how to read from them.
in your config file:
<appSettings>
<add key="ApplicationDirectory" value="C:\MyCompany\RunningFolder" />
<add key="Rounding" value="0.5" />
</appSettings>
In your code:
var ApplicationDirectory = ConfigurationManager.AppSettings["ApplicationDirectory"];
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx
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
I'm trying to add basic authenthication to my webservice. I followed steps from this article and ended up with this in my web.config file:
<configuration>
<httpModules>
<add name="BasicAuthenticationModule"
type="Mono.Http.Modules.BasicAuthenticationModule, Mono.Http, Version=2.0.0.0, PublicKeyToken=0738eb9f132ed756"/>
</httpModules>
<appSettings>
<add key="Authentication" value="Basic" />
<add key="Basic.Users" value="/home/vadmin/Projects/TestService/TestService/users.xml" />
<add key="Basic.Realm" value="My Realm" />
</appSettings>
</configuration>
My users.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user name="adrian" password="adrian">
<role name="user" />
</user>
</users>
When I run xsp2 and then go to
http://localhost:8080/TestService.asmx
user and password prompt appears. But after I enter correct user and password it asks me again and again. I'm pretty sure that path to users.xml file is correct, tried running xsp2 with --verbose options hoping for some error messages with no luck.
Can anyone help me debug this situation?
If you specify a path starting with / in config file, this will be interpreted not as a root directory of your filesystem, but as a root directory of your website - i.e. /home/vadmin/Projects/TestService/TestService.
So the path starting with / should be relative to website root folder - in your case this will be "/users.xml" if users.xml file is in project folder.
I'm creating a Custom tag in my web.config. I first wrote the following entry under the configSections section.
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
But, when I try to create a castle node inside the configuration node as below
<castle>
<components>
</components>
</castle>
I get the following error message:"*Could not find schema information for the element '**castle'*." "***Could not find schema information for the element '**components'***."
Am I missing something? I can't find why. And, if I run the application anyway, I get the following error "Could not find section 'Castle' in the configuration file associated with this domain."
Ps.// The sample comes from "Pro ASP.NET MVC Framework"/Steven Sanderson/APress ISBN-13 (pbk): 978-1-4302-1007-8" on page 99.
Thank you for the help
============================================================
Since I believe to have done exactly what's said in the book and did not succed, I ask the same question in different terms. How do I add a new node using the above information?
=============================================================================
Thank you. I did what you said and do not have the two warnings. However, I've go a big new warning:
"The element 'configuration' in namespace 'MyWindsorSchema' has invalid child element 'configSections' in namespace 'MyWindsorSchema'. List of possible elements expected: 'include, properties, facilities, components' in namespace 'MyWindsorSchema'."
What you get is not an error that will prevent you from running your application. It is just a warning that Visual Studio emits because it does not know the castle node in a config file. You could use a schema to enable intellisense. Download the Castle Windsor Schema file and take a look at the readme.txt inside. It tells you to put windsor.xsd somewhere on your hard drive and then reference it in the config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="MyWindsorSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="MyWindsorSchema file://S:\Common\Windsor\windsor.xsd">
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
</components>
</castle>
</configuration>