Monday, June 21, 2010

Loading xml from string

XML Document Approach

Let see how we can load xml from a string to a xmldocument
object
Step 1 Create XmlDocument Object
XmlDocument doc = new
XmlDocument();
Step 2
Creat a StringReader Object
System.IO.StringReader sr = new
System.IO.StringReader("XML
Sring");
Step 3 Create
XmlReader Object
XmlReader xmlrd =
XmlReader.Create(sr)

Here we are calling XmlReader.Create method and passing a
StringReader object
Step 4 Load xml
doc.Load(xmlrd);
Uses Load method of
XmlDocument object and pass XmlReader

XDocument Approach
XDocument.Parse("StringXML", LoadOptions.PreserveWhitespace);
In Xdocument you can directly pass the string and specify the load
options.Even you can directly parse into a XElement
XElement.Parse("StringXML")
You could also specify the declaration for your xml when you are creating the XDocument.Look at the following line
XDocument xdoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "YES"), XElement.Parse("StringXML"));

Here XDeclaration you can specify version of xml,encoding you are using and standalone or not

Friday, May 21, 2010

Diagnostics and Logging In ASP.NET

I was playing diagnostic options in asp.net so thought of sharing the information with others.
Scenario
* Log all warnings or above to the event log (So the people will notice it)
* Log all information statements and warnings to text files in the application's
directory.(It will help in detail to idenfy the issue)

<system.diagnostics>

<sources>

<source name="DefaultSource" switchName="DefaultSwitch">

<listeners>

<add name="FileLog"/>

<add name="EventLog"/>

</listeners>

</source>

</sources>

<switches>

<add name="DefaultSwitch" value="Information" />

</switches>

<sharedListeners>

<add name="FileLog"

type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"

initializeData="FileLogWriter"/>

<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="Test Application">

<filter type="System.Diagnostics.EventTypeFilter"

initializeData="Warning" />

</add>

</sharedListeners>

</system.diagnostics>