How to parse app.config using ConfigurationManager? - c#

I was using a certain method for parsing my app.config file. Then I was told that using ConfigurationManager is better and simpler. But the thing is I don't know how to do it with ConfigurationManager.
My original code looked like this:
XmlNode xmlProvidersNode;
XmlNodeList xmlProvidersList;
XmlNodeList xmlTaskFactoriesList;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("app.config");
xmlProvidersNode = xmlDoc.DocumentElement.SelectSingleNode("TaskProviders");
xmlProvidersList = xmlProvidersNode.SelectNodes("TaskProvider");
foreach (XmlNode xmlProviderElement in xmlProvidersList)
{
if (xmlProviderElement.Attributes.GetNamedItem("Name").Value.Equals(_taskProvider))
{
xmlTaskFactoriesList = xmlProviderElement.SelectNodes("TaskTypeFactory");
foreach (XmlNode xmlTaskFactoryElement in xmlTaskFactoriesList)
{
if (xmlTaskFactoryElement.Attributes.GetNamedItem("TaskType").Value.Equals(_taskType))
{
taskTypeFactory = xmlTaskFactoryElement.Attributes.GetNamedItem("Class").Value;
}
}
}
}
What would be the equivalent using ConfigurationManager? (Because all I can see is how to get keys not nodes..)
Thanks

Create a class that inherits ConfigurationSection called, say, MyConfigSection. Then you can use the ConfigurationManager.GetSection method to get an instance of your MyConfigSection class. The ConfigurationManager will do all the parsing, so you will have a strongly typed object to work with. Here is an excellent example to follow.

If you are concerned about the custom sections create your own class using Configuration section class. Here is an example about using it.

Related

Rss20FeedFormatter and RRS2 extensions

I want to use the RSS2 extensions feature to add my own non-standard elements to my RSS feed as described here:
http://cyber.law.harvard.edu/rss/rss.html#extendingRss:
However I don't think that the .Net Rss20FeedFormatter class supports this feature.
My code looks something like this:
public Rss20FeedFormatter GetRSS()
{
var feed = new SyndicationFeed(....);
feed.Items = new List<SyndicationItem>();
// add items to feed
return new Rss20FeedFormatter(feed);
}
If it doesn't support it is there any alternative to just creating the XML element by element?
Here's my findings. Took me a while to figure it all out.
This is what you do, your feed has to have a namespace
XNamespace extxmlns = "http://www.yoursite.com/someurl";
feed.AttributeExtensions.Add(new XmlQualifiedName("ext", XNamespace.Xmlns.NamespaceName), extxmlns.NamespaceName);
feed.ElementExtensions.Add(new XElement(extxmlns + "link", new XAttribute("rel", "self"), new XAttribute("type", "application/rss+xml")));
return new Rss20FeedFormatter(feed, false);
Your items need to be a derived class, and you write the extended properties in WriteElementExtensions, making sure you prefix them with the namespace (you don't have to but this is what is required to make it valid RSS).
class TNSyndicationItem : SyndicationItem
protected override void WriteElementExtensions(XmlWriter writer, string version)
{
writer.WriteElementString("ext:abstract", this.Abstract);
writer.WriteElementString("ext:channel", this.Channel);
}
The extended properties are ignore if you look in an RSS reader such as firefox, you'll need to write code to read them as well.
The url http://www.yoursite.com/someurl doesn't have to exist, but you need it to define the namespace and make the RSS valid. Normally you'll just put a page there which says something about what the feed should look like.

How do I use XmlSerializer to handle different namespace versions?

I am using the .NET XmlSerializer class to deserialize GPX files.
There are two versions of the GPX standard:
<gpx xmlns="http://www.topografix.com/GPX/1/0"> ... </gpx>
<gpx xmlns="http://www.topografix.com/GPX/1/1"> ... </gpx>
Also, some GPX files do not specify a default namespace:
<gpx> ... </gpx>
My code needs to handle all three cases, but I can't work out how to get XmlSerializer to do it.
I am sure there must be a simple solution because this a common scenario, for example KML has the same issue.
I have done something similar to this a few times before, and this might be of use to you if you only have to deal with a small number of namespaces and you know them all beforehand. Create a simple inheritance hierarchy of classes, and add attributes to the different classes for the different namespaces. See the following code sample. If you run this program it gives the output:
Deserialized, type=XmlSerializerExample.GpxV1, data=1
Deserialized, type=XmlSerializerExample.GpxV2, data=2
Deserialized, type=XmlSerializerExample.Gpx, data=3
Here is the code:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
[XmlRoot("gpx")]
public class Gpx {
[XmlElement("data")] public int Data;
}
[XmlRoot("gpx", Namespace = "http://www.topografix.com/GPX/1/0")]
public class GpxV1 : Gpx {}
[XmlRoot("gpx", Namespace = "http://www.topografix.com/GPX/1/1")]
public class GpxV2 : Gpx {}
internal class Program {
private static void Main() {
var xmlExamples = new[] {
"<gpx xmlns='http://www.topografix.com/GPX/1/0'><data>1</data></gpx>",
"<gpx xmlns='http://www.topografix.com/GPX/1/1'><data>2</data></gpx>",
"<gpx><data>3</data></gpx>",
};
var serializers = new[] {
new XmlSerializer(typeof (Gpx)),
new XmlSerializer(typeof (GpxV1)),
new XmlSerializer(typeof (GpxV2)),
};
foreach (var xml in xmlExamples) {
var textReader = new StringReader(xml);
var xmlReader = XmlReader.Create(textReader);
foreach (var serializer in serializers) {
if (serializer.CanDeserialize(xmlReader)) {
var gpx = (Gpx)serializer.Deserialize(xmlReader);
Console.WriteLine("Deserialized, type={0}, data={1}", gpx.GetType(), gpx.Data);
}
}
}
}
}
Here's the solution I came up with before the other suggestions came through:
var settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
using (var reader = XmlReader.Create(filePath, settings))
{
if (reader.IsStartElement("gpx"))
{
string defaultNamespace = reader["xmlns"];
XmlSerializer serializer = new XmlSerializer(typeof(Gpx), defaultNamespace);
gpx = (Gpx)serializer.Deserialize(reader);
}
}
This example accepts any namespace, but you could easily make it filter for a specific list of known namespaces.
Oddly enough you can't solve this nicely. Have a look at the deserialize section in this troubleshooting article. Especially where it states:
Only a few error conditions lead to exceptions during the
deserialization process. The most common ones are:
•The name of the
root element or its namespace did not match the expected name.
...
The workaround I use for this is to set the first namespace, try/catch the deserialize operation and if it fails because of the namespace I try it with the next one. Only if all namespace options fail do I throw the error.
From a really strict point of view you can argue that this behavior is correct since the type you deserialize to should represent a specific schema/namespace and then it doesn't make sense that it should also be able to read data from another schema/namespace. In practice this is utterly annoying though. File extenstion rarely change when versions change so the only way to tell if a .gpx file is v0 or v1 is to read the xml contents but the xmldeserializer won't unless you tell upfront which version it will be.

"An object reference is required for the non-static field, method, or property"

i am stuck with this small problem in my code.
I am trying to make small console application which will write into xml document.
I have used xmldocument and xmlnode concept.
ERROR i am getting is;
*An object reference is required for the non-static field, method, or property 'Write_xml.Program.give_node(System.Xml.XmlDocument)' C:\Documents and Settings\Administrator\Desktop\Write_xml\Write_xml\Program.cs*
code is okay except 1 error. I am not able to resolve it ,i want somebody to check it and correct it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace Write_xml
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
XmlDocument lets = new XmlDocument();
string path = #"D:\XMLFile.xml";
doc.Load(path);
XmlNode Rootnode = doc.SelectSingleNode("Number");
XmlNode TakenOde = give_node(doc);
Rootnode.AppendChild(TakenOde);
doc.Save(path);
}
public XmlNode give_node(XmlDocument lets)
{
// On this xmldoc we will perform XMLNODE operations
// for creat new nods and append child nodes
//XmlNode RootNode = xmldoc.CreateElement("Root");
XmlNode PersonsNode = lets.CreateElement("Person");
XmlNode NameNode = lets.CreateElement("Name");
PersonsNode.AppendChild(NameNode);
NameNode.InnerText = "1st";
XmlNode AgeNode = lets.CreateElement("Age");
PersonsNode.AppendChild(AgeNode);
AgeNode.InnerText = "2nd";
XmlNode CityNode = lets.CreateElement("City");
PersonsNode.AppendChild(CityNode);
CityNode.InnerText = "3rd";
return PersonsNode;
}
}
}
please let me what small mistake i am doing.
You're trying to call an instance method, but without specifying an instance.
The simplest fix for this is to make the give_node method static.
I haven't looked at the rest of the code to see whether it's okay or not, although give_node should be called GiveNode to follow .NET naming conventions.

How to read nested configuration element from the web.config?

I need to read configuration elements from the web.config.
Let this be my web.config.
<family>
<parents>
<child name="Hello"/>
<child name="World"/>
</parents>
<parents>
<child name="Hello1"/>
<child name="World2"/>
</parents>
</family>
So I have something like this, I need to read this into a collection.
How can i do this????
In general, you can store simple application settings and connection string in web.config (or app.config), but anything more complex, like an object graph or XML (as in your case) and you should consider a different method.
These may be helpful:
How do I store an XML value in my .NET App.Config file
(it suggests encoding the XML in an app setting)
However it would be better to have a separate XML data file and convert it to an object graph using Linq-To-XML (see reference) or XPath and the XmlDocument and related classes.
Edit: see the other answer, which does allow XML in the config file. That's a more direct answer to your exact questions but I will leave this here for reference. On the whole it looks like your data is not configuration data (more like runtime / user data) and does not belong in a .config file: so I would recommend storing it in a separate XML file, and having a config file entry pointing to the filename of the separate XML file.
Hope that helps!
You need to define your own custom configuration section, which will allow you to read the nested configuration element properly. BTW, this is the same method that all the others use, for instance the Enterprise Library components, NHibernate, etc.
The steps you need to take are very straightforward, and a tutorial is provided here:
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
You need to use the ConfigurationElementCollection Class.
See this sample on the MSDN
public struct Child
{
public string name;
public Child(string name)
{
this.name = name;
}
}
public class Parent
{
public List<Child> childs = new List<Child>();
public static List<Parent> ReadParentsFromXml(string fileName)
{
List<Parent> parents = new List<Parent>();
System.Xml.XmlTextReader doc = new System.Xml.XmlTextReader(fileName);
Parent element = new Parent();
while (doc.Read())
{
switch (doc.Name)
{
case "parents":
if (doc.NodeType == System.Xml.XmlNodeType.EndElement)
{
parents.Add(element);
element = new Parent();
}
break;
case "child":
if(doc.NodeType != System.Xml.XmlNodeType.EndElement)
element.childs.Add(new Child(doc.GetAttribute(0)));
break;
}
}
return parents;
}
}

Xml Attribute not showing up on ListBox - C#

I'm getting a null reference exception whenever it tries to add the packages titles info and other attributes but the attributes exist and the proper package is selected
Heres the code:
private void categorylist_listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
XmlDocument LoadPackageList = new XmlDocument();
//Removes the text "Select A Category" and refrehes the form
packagelist_listbox.Items.Remove(SelectaCategory_listbox);
if (categorylist_listview.SelectedItem == WWW_listviewitem)
{
LoadPackageList.Load("www.xml");
XmlNodeList WWWPackageList = LoadPackageList.SelectNodes("/Packages/*");
int countthenodes = 0;
foreach (XmlNode WWWPackages in WWWPackageList)
{
//Cycles through all the packages and assings them to a string then adds it to the packagelist
countthenodes++;
PackageTitle[countthenodes] = WWWPackages.Attributes["title"].ToString();
PackageInfo[countthenodes] = WWWPackages.Attributes["info"].ToString();
PackageDownloadUrl[countthenodes] = WWWPackages.Attributes["downloadurl"].ToString();
PackageTags[countthenodes] = WWWPackages.Attributes["tags"].ToString();
packagelist_listbox.Items.Add(PackageTitle[countthenodes]);
}
Refresh(packagelist_listbox);
}
}
It Errors out at PackageTitle[countthenodes] = WWWPackages.Attributes["title"].ToString();
XML File:
<Packages>
<Firefox title="Mozilla Firefox" tags="www firefox web browser mozilla" info="http://google.com" downloadurl="http://firefox.com"></Firefox>
</Packages>
The Variables are declared
public string[] PackageTags;
public string[] PackageTitle;
public string[] PackageInfo;
public string[] PackageDownloadUrl;
At the very beginning of the file
Well, the first problem is that calling ToString() on an XmlAttribute isn't going to do what you want. You should use the Value property. However, I don't believe that's causing a NullReferenceException unless the data isn't quite as you showed it. Here's a short but complete program which works fine:
using System;
using System.Xml;
class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNodeList list = doc.SelectNodes("/Packages/*");
foreach (XmlNode node in list)
{
Console.WriteLine(node.Attributes["title"].Value);
}
}
}
That displays "Mozilla Firefox" with the XML you gave us.
Options:
Your real XML actually contains an element without a title attribute
Perhaps PackageTitle is null?
It would help if you could produce a short but complete program demonstrating the problem. Ideally it should avoid using a GUI - I can't see anything here which is likely to be GUI-specific.
If you could tell us more about PackageTitle and how it's being initialized, that would help too. How are you expecting it to just keep expanding for as many elements as you find? Or is it an array which is initialized to a larger size than you ever expect to find elements?

Categories

Resources