My XmlTextReader cheat sheet!
I use this little cheat sheet to remember XmlTextReader class details.
I use this little cheat sheet to remember XmlTextReader class details.
Here are the steps to design an xml file and serialise it into an object with validation:
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.
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 < and > becomes > ! 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:
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!