Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

app.config custom types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ink - 22 Oct 2007 12:56 GMT
Hi all,

Is this possable, and how if so?

i have created 3 classes that i have then Serialized to xml and placed in a
textbox. i can then read the text from the textbox and Deserialize it back
into a new object. this to me proves that the classes and the xml are valid.

But when i take that same xml and past it into the App.Config file and use
the
Configuration.GetSection("ImportFormat").SectionInformation.GetRawXml();  to
read it from the config file and then Deserialize it i get the following
error.

-  ex {"There is an error in XML document (0, 0)."} System.Exception
{System.InvalidOperationException}
-  InnerException {"The type initializer for
'System.Xml.Serialization.XmlSerializationReader' threw an exception."}
System.Exception {System.TypeInitializationException}
-  InnerException {"Configuration system failed to initialize"}
System.Exception {System.Configuration.ConfigurationErrorsException}
+  InnerException {"Unrecognized configuration section ImportFormat.
(C:\\Learn\\Csharp\\Desk\\AppConfigTest\\AppConfigTest\\AppConfigTest\\bin\\Debug\\AppConfigTest.vshost.exe.config
line 4)"} System.Exception
{System.Configuration.ConfigurationErrorsException}

Is what i am trying to do not possable?

i have pasted app.config below.

thanks,
ink

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<ImportFormat>
 <ImportRecord TableName="GoodsExpected" RowIdentifier="H"
IndexOfInsertion="0" NoOfRowsPerRecord="5">
  <FieldData ColumnName="STORAGE PROVIDER" FieldName="CompanyID"
ColumnIndex="1" RowIndex="0" Required="true" />
  <FieldData ColumnName="WAREHOUSE" FieldName="WarehouseID" ColumnIndex="2"
RowIndex="1" Required="true" />
  <FieldData ColumnName="ASN REFERENCE" FieldName="ReferenceID"
ColumnIndex="2" RowIndex="2" Required="true" />
  <FieldData ColumnName="EXPECTED DATE" FieldName="ExpectedDate"
ColumnIndex="2" RowIndex="3" Required="true" />
  <FieldData ColumnName="CUSTOMERID" FieldName="SupplierID" ColumnIndex="2"
RowIndex="4" Required="true" />
 </ImportRecord>
 <ImportRecord TableName="GoodsReceivedBatch" RowIdentifier="D"
IndexOfInsertion="1" NoOfRowsPerRecord="1" HeaderRowIdentifier="H">
  <FieldData ColumnName="PALLET ID" FieldName="PalletID" ColumnIndex="1"
RowIndex="0" Required="false" />
  <FieldData ColumnName="ITEM CODE" FieldName="ItemID" ColumnIndex="2"
RowIndex="0" Required="true" />
  <FieldData ColumnName="QTY" FieldName="Qty" ColumnIndex="3" RowIndex="0"
Required="true" />
  <FieldData ColumnName="ITEM DESCRIPTION" FieldName="Description"
ColumnIndex="4" RowIndex="0" Required="false" />
 </ImportRecord>
</ImportFormat>

</configuration>
sloan - 22 Oct 2007 13:03 GMT
When you write a "Custom Config Section", you gotta write a "Custom Config
Handler".

..

This entry shows how to write a custom configuration handler (based on some
xml in the config file).
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!138.entry

Search for "Handler" and "Settings" in the downloadable code.

Here are some xml tidbits as well.
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

You need to go through and understand the FIRST URL above first.
There are some MS links in the headers of some of the source files as well.

..

> Hi all,
>
[quoted text clipped - 61 lines]
>
> </configuration>
ink - 22 Oct 2007 13:15 GMT
thanks heaps Sloan, i knew there must have been something i was missing.

> When you write a "Custom Config Section", you gotta write a "Custom Config
> Handler".
[quoted text clipped - 81 lines]
>>
>> </configuration>
sloan - 22 Oct 2007 15:16 GMT
No problem.

Keep in mind my sample is not a direct KB on "how to implement a custom
handler".

I actually have written one to use with my smtp settings thingy.

//Quote from article//
You can download the code HERE. (Right-Click and "Save As" works best)
//End Quote

After you get the code, find this class:
EmailSmtpSettingsHandler : IConfigurationSectionHandler

and throw a break point in there.

You're basically taking xml, and writing xpath statements to populate some
other object.
Aka, you gotta write the glue code which takes XML and converts it to a real
object.

Now, in your case, you're gonna take the xml, and then try and use it for a
deserialization.
Thus the second URL might come in handy.

............

> thanks heaps Sloan, i knew there must have been something i was missing.
>
[quoted text clipped - 83 lines]
>>>
>>> </configuration>
sam01m - 23 Oct 2007 02:55 GMT
I just went through this myself... here is the solution that I came up with...

.........................................App.config
..............................................

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
   <section name="MyCustomSection" type="My.Custom.Section.Handler,
My.Custom.Section.Handler.Assembly, Version=1.0.0.0,Culture=neutral,
PublicKeyToken=bd1505632153fa83" />
 </configSections>

 <MyCustomSection Username="MyUserName" Password="MyPassword" />

</configuration>

...................................My.Custom.Section.Handler.Assembly......................................

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace My.Custom.Section
{
   public class Handler: ConfigurationSection
   {
       [ConfigurationProperty("Username", DefaultValue = "", IsRequired =
true)]
       public string Username
       {
           get
           {
               return (string)this["Username"];
           }
           set
           {
               this["Username"] = value;
           }
       }

       [ConfigurationProperty("Password", DefaultValue = "", IsRequired =
true)]
       public string Password
       {
           get
           {
               return (string)this["Password"];
           }
           set
           {
               this["Password"] = value;
           }
       }
   }
}

.........................................Using the Custom
Section..........................................

using My.Custom.Section;

public class MyClass
{
    public void MyMethod()
    {
         Handler section = (Handler)config.Sections["MyCustomSection"];

         string username = section.Username;
         string password = section.Password;
     }
}

.............................Conclusion...................................

You could also do things like section encryption, etc... like this

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Handler section = (Handler)config.Sections["MyCustomSection"];

if (!section.SectionInformation.IsProtected)
{
   
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
    section.SectionInformation.ForceSave = true;
    config.Save(ConfigurationSaveMode.Full);
}

// this is the information I wish I had when I was looking for answers, hope
it helps
ink - 24 Oct 2007 10:48 GMT
Thanks for this Sam.

i have managed to get it working by putting a Dummy Handler in that does
nothing and the Desterilizing the Raw XML of the section.
Your way is allot more elegant so i think i may implement your solution.

thanks,
ink

>I just went through this myself... here is the solution that I came up
>with...
[quoted text clipped - 91 lines]
> hope
> it helps

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.