Create XSD schema from custom XML file - c#

I have XML looking like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="profile">
<section name="server1" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=a1a1a1a1a1a1a1a1, Custom=null" />
<section name="server2" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=a1a1a1a1a1a1a1a1, Custom=null" />
<section name="server3" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=a1a1a1a1a1a1a1a1, Custom=null" />
</sectionGroup>
</configSections>
<profile>
<server1>
<add key="server" value="serverName1" />
<add key="check" value="0" />
<add key="user" value="sa" />
<add key="psw" value="secret" />
<add key="show" value="1" />
</server1>
<server2>
<add key="server" value="serverName2" />
<add key="check" value="0" />
<add key="user" value="sa" />
<add key="psw" value="secret" />
<add key="show" value="1" />
</server2>
<server3>
<add key="server" value="serverName3" />
<add key="check" value="0" />
<add key="user" value="sa" />
<add key="psw" value="secret" />
<add key="show" value="1" />
</server3>
</profile>
</configuration>
and I would like to create XSD schema for its validation. I have tried it with online XSD generators like https://www.freeformatter.com/xsd-generator.html with any of 3 types of XSD Design but the problem is that validation will not pass if there is any other number of servers in XML beside 3.
Any help or advice for software or other online XSD generators which can account for this randomness is very much appreciated?

There is no way in XSD (other than XSD 1.1 assertions) to say 'the children of this element must be named "server1", "server2", ... "serverN" in order'. XSD is a bit paternalistic like this: it's not designed to describe any XML you might dream up, it's designed to describe well-designed XML. You should drop the digit and call all the elements "server"; or if the digit serves some purpose (e.g. if it's retained after some server elements are deleted from the XML), then make it an attribute value: <server id="server1".../>

I've always found that XSD.exe does a pretty good job. This validator is also pretty good too. On top of that you could try the XML Tools plugin in Notepad++.
With complex XML documents it's often easier to build them up slowly, so start with a simplified version and then gradually add other parts.
This tutorial and this one are both very good and are definitely worth looking at if you're struggling with xsds.
I suggest you have a look at the tutorials and then maybe rethink what you want and restructure your xml accordingly. An obvious first question might be how do server1, server2, and server3 differ? If they're all the same you want three instances of server; if they're different you probably want to capture that somehow in your xml.

Related

How to retrieve the value for the Key in the Web.Config using DictionarySectionHandler

How to fetch the corresponding value of a key that I would get dynamically. I wish to use the system defined DictionarySectionHandler to do the job, of fetching the data from my custom built config section in the Web.config file
Code block in Web.Config
<section name="domainsource" type="System.Configuration.DictionarySectionHandler"/>
<domainSource>
<add key="0" value="170" />
<add key="1" value="171" />
<add key="2" value="172" />
<add key="3" value="173" />
<add key="12" value="174" />
</domainSource>
Sourcecode in the main cs file from where I wish to retrieve the data from the Web.Config
Hashtable statusCodes = ConfigurationManager.GetSection("domainSource") as Hashtable;
vDomainSource = statusCodes[vDomainID];
This is where I am stuck vDomainID would be a value 0/1/2/3/12, based on this value I need to fetch its respective Source from the Web.Config. Any help on this aspect would be really appreciated.
You have a missspelling in the defintion of the section domainsource -> domainSource. Further ensure that the elemnt is defined in an element. Then it should work.
<configuration>
<configSections>
<section name="domainSource" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<domainSource>
<add key="0" value="170" />
<add key="1" value="171" />
<add key="2" value="172" />
<add key="3" value="173" />
<add key="12" value="174" />
</domainSource>
</configuration>

Paypal: Configuration.GetApiContext -> VS: Cannot resolve Issue

If I install .Net SDK and uses Sample Code, VS says: Cannot resolve symbol 'Configuration' at this line:
var apiContext = Configuration.GetAPIContext();
So, what DLL or namespaces did I missed?
It is a wrapper class around the configuration section.
Please look into \PayPal-NET-SDK-develop\Samples\Source\Utilities\Configuration.cs and create it to suit your configuration.
Don't forget to modify your web.config or app.config
<configSections>
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<!-- PayPal SDK settings -->
<paypal>
<settings>
<add name="mode" value="sandbox" />
<add name="clientId" value="youclientid" />
<add name="clientSecret" value="yoursecret" />
</settings>
</paypal>

Define multiple sinks with different outputTemplates based on logging level in xml config for Serilog

Is there any way to define multiple sinks in the XML config for serilog that will allow the outputTemplate to change based on the level of log?
I currently have:
<add key="serilog:minimum-level" value="Verbose" />
<add key="serilog:write-to:ColoredConsole" />
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Logs\AdapterService-{Date}.txt" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="[{Timestamp:G}] [{Level}] [{SourceContext:l}] {Message}{NewLine:l}{Exception:l}" />
However would like the Debug level logs to not include the SourceContext string as in:
<add key="serilog:write-to:RollingFile.outputTemplate" value="[{Timestamp:G}] [{Level}] {Message}{NewLine:l}{Exception:l}" />
I am aware you can set the restrictedToMinimumLevel: LogEventLevel.Verbose but I am unsure how to do this in the XML.

can not find schema info when configuration quartz.net

When i add this code to app.config in Windows service program:
<quartz>
<add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="10"/>
<add key="quartz.threadPool.threadPriority" value="2"/>
<add key="quartz.jobStore.misfireThreshold" value="60000"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
it tips:
Could not find schema information for the element `key`.
Could not find schema information for the attribute `quartz`.
Could not find schema information for the element `value`.
I am sure i have using job_scheduling_data_2_0.xsd file in background.
And predefine the quartz:
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
Where is the problem?And where to find the schema info?
And when is the label <quartz> namespace?
Where should i find the xsd file of quartz.net?
Normal reason is your config file lack of schema file like *.xsd in:..\Microsoft Visual Studio\Xml\Schemas folder.
You can omit these tips,it could not affect your program running.
Or you can write the xsd schema file by yourself.

How to log all error in DB but email errors only on conditional basis?

I want email to be sent only on a specific condition and log error in DB in all cases. But as I understand, filtering can't work for one of the two. Is that right? If so then how can I achieve it?
Also to note that, right now I'm saving additional info to database on ErrorMail_Mailing in global.asax as replied by Atif Aziz. Because email will be sent only on conditional basis and ErrorMail_Mailing fires only while sending email, I wonder how would I be able to save additional info of all errors to database.
UPDATE:I have modified Elmah code a bit to satisfy my need.
The first step is to configure modules. Make sure you add Elmah.ErrorFilterModule after any of the logging modules from ELMAH, as shown here with ErrorLogModule:
<httpModules>
...
//email
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
//sql
<add name="ErrorSql" type="Elmah.SqlErrorLog, Elmah"/>
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
...
</httpModules>
Then in your configuration section registered Elmah.ErrorFilterSectionHandler as shown here:
<configSections>
<configSections>
<sectionGroup name="elmah">
<section name="errorFilter" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
</sectionGroup>
</configSections>
Now you can add filters to decide what errors to be ignored for what source. The following example shows how to prevent having 404 HTTP errors being mailed.
<elmah>
<errorMail from="xx#xx.com" fromName="xx" to="xx#xx.com" subject="An unhandled exception occured xxx" priority="Normal" async="false" smtpServer="xx.xx.xx.com"/>
//sql
<errorLog name="ErrorSql" type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
<errorFilter>
<test>
<and>
<equal binding="HttpStatusCode" value="404" type="Int32" />
<regex binding="FilterSourceType.Name" pattern="mail" />
</and>
</test>
</errorFilter>
</elmah>
You can find out more detail information on the following link.
http://code.google.com/p/elmah/wiki/ErrorFiltering
The ELMAH documentation on error filtering has a section on exactly your scenario and is called, which amounts to filtering by source. For example, the following will prevent 404 HTTP errors from being mailed but they will be still logged (assuming both mailing and logging modules are registered):
<errorFilter>
<test>
<and>
<equal binding="HttpStatusCode" value="404" type="Int32" />
<regex binding="FilterSourceType.Name" pattern="mail" />
</and>
</test>
</errorFilter>
If you want to save all exceptions to the database you should just be able to use the ErrorLogModule like so which should be independent of what you are doing in the error mail module:
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
and then in your elmah secition of your config:
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />
You should try out the StackExchange.Exceptional
This project was inspired by ELMAH, but it didn't suit our particular
needs for very, very high volume error logging when a network-level
event occurs.
StackExchange.Exceptional is the error handler used internally by
Stack Exchange and Stack Overflow for logging to SQL.
It also supports JSON and memory error stores, filtering of exceptions
before logging, and fail/retry mechanisms for storing errors if
there's an interruption in connecting to the error store.
It's highly customizable and it's really easy to add something according your needs.
As i can see an pull request has the email functionality implemented Email functionality so you can start from there.
To set it up you only need to look at the web.config and pick what to enable.
Hope it helps.

Categories

Resources