Archive

Archive for the ‘XML’ Category

My XmlTextReader cheat sheet!

February 7th, 2005 Ali Moeen No comments

I use this little cheat sheet to remember XmlTextReader class details.


Click to see larger size, It is a little big.

Categories: XML Tags:

Validating the xml while serialization by using XmlValidatingReader

March 25th, 2003 Ali Moeen No comments

Here are the steps to design an xml file and serialise it into an object with validation:



  1. Create the xml file in VS.NET IDE.
  2. While xml file is open in IDE, from the menu bar, choose XML-> Create Schema.
  3. The generated schema is probably too wide. Narrow data-types in the XSD schema editor.
  4. When XSD is ready, we need to generate the data class. VS.NET IDE only generates DataSet for that XSD (which is typical choice in most situations). To generate the data class, we can use XSD.EXE inside .net framework SDK’s BIN folder. The command line is something like this:
    XSD test.xsd /language:cs
    for more information about xsd.exe, see MSDN documentation.
  5. Here is the serialization/validation code:

try

{

    string strXmlPath = Server.MapPath(“/Thumb/Config.xml”);

    string strXsdPath = Server.MapPath(“/Thumb/Config.xsd”);

    XmlSerializer serializer = new XmlSerializer(typeof(Config));

    XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(strXmlPath));

    XmlSchemaCollection xsc = new XmlSchemaCollection();

    xsc.Add(“”, strXsdPath);

    reader.Schemas.Add(xsc);

    Config config = (Config)serializer.Deserialize(reader);

    reader.Close();

    // Now the Config Object contains vlidated data

    // …

}

catch (Exception e1)

{

    throw (e1);

}

If we want to do similar thing by using DataSet, this code will do it:


try

{

    string strXmlPath = Server.MapPath(“/Thumb/Config.xml”);

    string strXsdPath = Server.MapPath(“/Thumb/Config.xsd”);

    DataSet ds = new DataSet();

    ds.ReadXmlSchema(strXsdPath);

    ds.ReadXml(strXmlPath);

    string s;

    s = ds.Tables["Config"].Rows[0]["Path"].ToString();

    Response.Write(s);

}

catch (Exception e1)

{

    throw (e1);

}


It is shorter; however, the validation error that this one returns is not as specific as XmlValidatingReader’s error.

Categories: C#, XML Tags:

I made the smallest RSS reader ever!

March 17th, 2003 Ali Moeen No comments

Perhaps the RSS reader that I use for my website is one of the smallest one. I used ASP.NET XML server control to merge RSS XML data with an XSL file. It works fine. The only challenge that I had was <xsl:value-of select="description"/> translates HTML tags to their escape code, so < becomes &lt; and > becomes &gt; ! And we see the actual html tags on the rendered data. The solution is using disable-output-escaping attribute so the value-of tag becomes like this:


<xsl:value-of select="description" disable-output-escaping="yes"></xsl:value-of>


The download is here:


RSS.zip


Here is an example shows the querystring names:


rss.aspx?RSS=http://moeen.com/blog/Rss.aspx&XSL=RSSBlog.xsl


There is no error handling. Shame!

Categories: ASP.NET, XML Tags: