Search

just show me the code

Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Tuesday, September 29, 2009

Xml Literals in vb

Here I am creating an XElement in C#

   19         public static XElement PropertyTable(List<Property> propertyList)
   20         {
   21             XElement root = new XElement("div");
   22             root.Add(new XElement("div"
   23                 , new XAttribute("class", "propertyCount hand")
   24                 , string.Format("{0} properties", propertyList.Count)));
   25             XElement tableElement = new XElement("table", new XAttribute("class", "propertyTable"));
   26             root.Add(tableElement);
   27             XElement headerRow = new XElement("tr");
   28             headerRow.Add(new XElement("th", "Code"));
   29             headerRow.Add(new XElement("th", "Value"));
   30             tableElement.Add(headerRow);
   32
   33             foreach (Property p in propertyList)
   34             {
   35                 XElement projectRow = new XElement("tr");
   36                 projectRow.Add(new XElement("td", p.Code));
   37                 projectRow.Add(new XElement("td", p.Value));
   38                 tableElement.Add(projectRow);
   39             }
   40             return root;
   41         }

Here I am creating the EXACT SAME XElement in VB using xml literals


   19     Public Function PropertyTable(ByVal propertyList As List(Of [Property])) As XElement
   20       Dim root = <div>
   21                    <div class="propertyCount hand"><%= String.Format("{0} properties", propertyList.Count) %></div>
   22                    <table class="propertyTable">
   23                      <tr>
   24                        <th>Code</th>
   25                        <th>Value</th>
   26                      </tr>
   27                      <%= From p In propertyList Select _
   28                        <tr>
   29                          <td><%= p.Code %></td>
   30                          <td><%= p.Value %></td>
   31                        </tr> %>
   32                    </table>
   33                  </div>
   34       Return root
   35     End Function  

which would you rather read?  

Saturday, January 31, 2009

xml membership

credit to Atrem XmlProviders download their code from codeplex

   54     <membership defaultProvider="XmlMembershipProvider">
   55       <providers>
   56         <clear/>
   57         <add applicationName="SampleWebsite" 
   58              name="XmlMembershipProvider" 
   59              type="Artem.Web.Security.XmlMembershipProvider" 
   60              minRequiredPasswordLength="1"
minRequiredNonAlphanumericCharacters="0"
   61              requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
   62              passwordFormat="Hashed"/>
   63       </providers>
   64     </membership>
   77     <roleManager enabled="true" cacheRolesInCookie="true" 
defaultProvider
="XmlRoleProvider">
   78       <providers>
   79         <clear/>
   80         <add applicationName="SampleWebsite" name="XmlRoleProvider" 
type
="Artem.Web.Security.XmlRoleProvider"/>
   81       </providers>
   82     </roleManager> 
   83     <profile enabled="true" defaultProvider="XmlProfileProvider">
   84       <providers>
   85         <clear/>
   86         <add applicationName="SampleWebsite" name="XmlProfileProvider"
type="Artem.Web.Security.XmlProfileProvider"/>
   87       </providers>
   88       <properties>
   89         <group name="Person">
   90           <add name="FirstName" type="String"/>
   91           <add name="LastName" type="String"/>
   92           <add name="Years" type="Int32"/>
   93         </group>
   94         <add name="PageCursor" type="String"/>
   95         <add name="Counter" type="Int32" defaultValue="0"/>
   96       </properties>
   97     </profile>

Wednesday, November 5, 2008

asp.net handlers serving xml

your handler code
namespace XXXX.TourAdmin.Web.Admin
{
    /// 
    /// Summary description for $codebehindclassname$
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class VideoHandler : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            context.Response.ContentType = "UTF-8";
            context.Response.Write(this.GetVideoXml());
        }
 
        protected string GetVideoXml()
        {
            VideoContext vc = new VideoContext();
            var videos = from v in vc.Videos
                         select new 
                         {
                             VideoClip = v.FileLocation,
                             Title = v.Title,
                             Date = v.VideoDate,
 
                         };
 
            XDocument xmlDoc = new XDocument();
            XElement xEleVideos = new XElement("videos");
            xmlDoc.Add(xEleVideos); 
            foreach (var v in videos)
            {
                xEleVideos.Add(new XElement("video",  new XAttribute("VideoClip", v.VideoClip),
                    new XAttribute("Title", v.Title), new XAttribute("Copy", v.Date)));
            }
            string s = xmlDoc.ToString();
            return s;
        }
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
here is the data:



TitleFileLocationVideoDateactive
Miami, Florida200809142009-02-12 00:00:001

now put the url in the address bar
http://localhost/XXXX.TourAdmin.Web/Admin/VideoHandler.ashx
<videos>
  <video VideoClip="20080914" Title="Miami, Florida" copy="2009-02-12T00:00:00" />
<videos>
Props to Lewis

Tuesday, November 4, 2008

Linq to xml

Linq (Language Integrated Query) works on xml too

<content>
  <gallery Name="All Videos">
    <video VideoClip="20081014.flv" Title="Orlando, Florida" Copy="Feb 12, 2009"/>
    <video VideoClip="20081121.flv" Title="Atlanta, Georgia" Copy="Feb 12, 2009"/>
  </gallery>
</content>



protected void LoadGallaryList()
{
    string xmlFile = this.Server.MapPath("~/Flash/_xml/video.xml"); 
    XDocument xmlDoc = XDocument.Load(xmlFile); 
    var videos = from v in xmlDoc.Descendants("video") // .Elements("video")
                 select new 
                 {
                     VideoClip = v.Attribute("VideoClip").Value,
                     Title = v.Attribute("Title").Value,
                     Date = v.Attribute("Copy").Value,
                 }; 
    foreach (var v in videos)
    {
        lblCurrent.Text += "title:" + v.Title + " VideoClip:" + v.VideoClip + " Date:" + v.Date + "<br/>";
    }
}

output:
title:Orlando, Florida VideoClip:20080914.flv Date:Feb 12, 2009
title:Atlanta, Georgia VideoClip:20081121.flv Date:Feb 12, 2009

Contributors