Saturday 29 June 2013

 Click Here to Download FactoryPatternSampleWithCSharp.zip C# Example.
 Click Here to Download FactoryPatternSampleWithVBNET.zip VB.Net Example.

Factory Design Pattern is type of creational patterns. We can architect software with the help of factory design pattern. With the help of factory pattern we can centralize creations of objects and also reduce object creation logic at the client side.

Let's see some example on this.
We take booklet display example. You have functionality like display booklet on screen in two ways one is with header and footer and other is plain without header and footer.

In traditional approach you might take two classes "BookletWithHeaderFooter" and "BookletPlain" and create objects depending on your requirement on client side like below code.

Tuesday 18 June 2013

We can Create or Generate XML file or document programmatically using XDocument, XElement and XAttribute class. We can use infinite level of XML element. We can also create "web.config" file programmatically.

Here is example for this.
In this example we create sample "web.config" file using XElement and XAttribute class and display that generated XML in textbox. We are using ".ToString" method of XDocument object to get XML string.

ASPX Code :
    <asp:TextBox runat="server" ID="txtXML" TextMode="MultiLine" Rows="20" Columns="110"></asp:TextBox>


C#. Net Example :
        XDocument myDocument = new XDocument(
                                  new XElement("configuration",
                                    new XElement("system.web",
                                      new XElement("membership",
                                        new XElement("providers",
                                          new XElement("add",
                                            new XAttribute("name","WebAdminMembershipProvider"),
                                            new XAttribute("type","System.Web.Administration.WebAdminMembershipProvider")))),
                                      new XElement("httpModules",
                                        new XElement("add",
                                          new XAttribute("name","WebAdminModule"),
                                          new XAttribute("type","System.Web.Administration.WebAdminModule"))),
                                      new XElement("authentication",
                                        new XAttribute("mode", "Windows")),
                                      new XElement("authorization",
                                        new XElement("deny",
                                          new XAttribute("users", "?"))),
                                      new XElement("identity",
                                        new XAttribute("impersonate", "true")),
                                      new XElement("trust",
                                        new XAttribute("level", "full")),
                                      new XElement("pages",
                                        new XAttribute("validationRequest", "true")))));

        txtXML.Text = myDocument.ToString();

VB.Net Examples :
        Dim myDocument As New XDocument(
            New XElement("configuration",
                                    New XElement("system.web",
                                        New XElement("membership",
                                        New XElement("providers",
                                            New XElement("add",
                                            New XAttribute("name", "WebAdminMembershipProvider"),
                                            New XAttribute("type", "System.Web.Administration.WebAdminMembershipProvider")))),
                                        New XElement("httpModules",
                                        New XElement("add",
                                            New XAttribute("name", "WebAdminModule"),
                                            New XAttribute("type", "System.Web.Administration.WebAdminModule"))),
                                        New XElement("authentication",
                                        New XAttribute("mode", "Windows")),
                                        New XElement("authorization",
                                        New XElement("deny",
                                            New XAttribute("users", "?"))),
                                        New XElement("identity",
                                        New XAttribute("impersonate", "true")),
                                        New XElement("trust",
                                        New XAttribute("level", "full")),
                                        New XElement("pages",
                                        New XAttribute("validationRequest", "true")))))

        txtXML.Text = myDocument.ToString()

Output :
Create or Generate XML file programmatically using XElement and XAttribute class
(To view original image , click on image)

Tuesday 11 June 2013

XML declaration of a XML document or file is very important. We can add XML declaration and create XML element programmatically .
We are using "XDocument" and "XDeclaration" class to add declaration.

Here is example for this.
In this example we create XML document with specified XML declaration and save it into the StringWriter object. You can see in the debug time screenshot.

C#. Net Example :
        XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-16", "Yes"),new XElement("Products"));
        StringWriter sw = new StringWriter();
        doc.Save(sw);
        Response.Write(sw.ToString());

VB.Net Examples :
        Dim doc As New XDocument(New XDeclaration("1.0", "UTF-16", "Yes"), New XElement("Products"))
        Dim sw As New StringWriter()
        doc.Save(sw)
        Response.Write(sw.ToString())

Output :
Add XML declaration programmatically
(To view original image , click on image)



Wednesday 5 June 2013

We can construct or create XML or XElement from string. There are situations in applications where we receive some XML data in form of string and we need to convert that string back into the XML for our need and easy to handle that XML document for querying and other manipulation.
We are using "XElement.Parse" method to get XElement.

Here is example for this.
In this example we take one string xml and then we converted that in to XElement using "Parse" method of "System.Xml.Linq.XElement" Class.
You can see in output image that after parse that object we watch this object in "Text Visualizer" window and able to see that that string is converted in to XElement.

C#. Net Example :
        string strXML = "<Products>" +
                        "<Product id='1' price='5000'>CPU</Product>" +
                        "<Product id='2' price='500'>Monitor</Product>" +
                        "<Product id='2' price='100'>Mouse</Product>" +
                        "</Products>";
        System.Xml.Linq.XElement objXElement = System.Xml.Linq.XElement.Parse(strXML);
        Response.Write(objXElement);

VB.Net Examples :
        Dim strXML As String = "<Products>" & _
                                "<Product id='1' price='5000'>CPU</Product>" & _
                                "<Product id='2' price='500'>Monitor</Product>" & _
                                "<Product id='2' price='100'>Mouse</Product>" & "</Products>"
        Dim objXElement As System.Xml.Linq.XElement = System.Xml.Linq.XElement.Parse(strXML)
        Response.Write(objXElement)

Output : 
Create XElement or XML from string
(To view original image , click on image)