public override void GenerateCode(AssemblyBuilder assemblyBuilder) { // get name and namespace from the filename string fname = VirtualPathUtility.GetFileName(VirtualPath); fname = fname.Substring(0, fname.LastIndexOf('.')); // no extension int i = fname.LastIndexOf('.'); string name, ns; if (i < 0) { name = fname; ns = string.Empty; } else { name = fname.Substring(i + 1); ns = fname.Substring(0, i); } // load as XML XmlDocument doc = new XmlDocument(); using (Stream s = OpenStream(VirtualPath)) { doc.Load(s); } // create the channel GenericRssChannel channel = GenericRssChannel.LoadChannel(doc); // compile the channel CodeCompileUnit ccu = new CodeCompileUnit(); RssCodeGenerator.GenerateCodeDomTree(channel, ns, name, ccu); assemblyBuilder.AddCodeCompileUnit(this, ccu); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { // load as XML XmlDocument doc = new XmlDocument(); using (Stream s = OpenStream(VirtualPath)) { doc.Load(s); } // valide root rssdl node XmlNode root = doc.DocumentElement; if (root.Name != "rssdl") { throw new InvalidDataException( string.Format("Unexpected root node '{0}' -- expected root 'rssdl' node", root.Name)); } // iterate through rss nodes for (XmlNode n = root.FirstChild; n != null; n = n.NextSibling) { if (n.NodeType != XmlNodeType.Element) { continue; } if (n.Name != "rss") { throw new InvalidDataException( string.Format("Unexpected node '{0}' -- expected root 'rss' node", root.Name)); } string name = string.Empty; string file = string.Empty; string url = string.Empty; string ns = string.Empty; foreach (XmlAttribute attr in n.Attributes) { switch (attr.Name) { case "name": name = attr.Value; break; case "url": if (!string.IsNullOrEmpty(file)) { throw new InvalidDataException("Only one of 'file' and 'url' can be specified"); } url = attr.Value; break; case "file": if (!string.IsNullOrEmpty(url)) { throw new InvalidDataException("Only one of 'file' and 'url' can be specified"); } file = VirtualPathUtility.Combine(VirtualPathUtility.GetDirectory(VirtualPath), attr.Value); break; case "namespace": ns = attr.Value; break; default: throw new InvalidDataException( string.Format("Unexpected attribute '{0}'", attr.Name)); } } if (string.IsNullOrEmpty(name)) { throw new InvalidDataException("Missing 'name' attribute"); } if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(file)) { throw new InvalidDataException("Missing 'url' or 'file' attribute - one must be specified"); } // load channel GenericRssChannel channel = null; if (!string.IsNullOrEmpty(url)) { channel = GenericRssChannel.LoadChannel(url); } else { XmlDocument rssDoc = new XmlDocument(); using (Stream s = OpenStream(file)) { rssDoc.Load(s); } channel = GenericRssChannel.LoadChannel(rssDoc); } // compile channel CodeCompileUnit ccu = new CodeCompileUnit(); RssCodeGenerator.GenerateCodeDomTree(channel, ns, name, ccu); assemblyBuilder.AddCodeCompileUnit(this, ccu); } }