StripIllegalXmlCharacters() public static method

Replaces illegal XML characters with a question mark (?).
Only strips illegal characters as per XML 1.0, not 1.1. See section 2.2 Characters of Extensible Markup Language (XML) 1.0 (Fourth Edition).
public static StripIllegalXmlCharacters ( string xml ) : string
xml string
return string
コード例 #1
0
ファイル: ErrorRssHandler.cs プロジェクト: yornstei/Elmah
        public static void ProcessRequest(HttpContextBase context)
        {
            const int pageSize = 15;
            var       entries  = new List <ErrorLogEntry>(pageSize);
            var       log      = ErrorLog.GetDefault(context);

            log.GetErrors(0, pageSize, entries);

            var response = context.Response;

            response.ContentType = "application/xml";

            var title = string.Format(@"Error log of {0} on {1}",
                                      log.ApplicationName, Environment.MachineName);

            var link    = ErrorLogPageFactory.GetRequestUrl(context).GetLeftPart(UriPartial.Authority) + context.Request.ServerVariables["URL"];
            var baseUrl = new Uri(link.TrimEnd('/') + "/");

            var items =
                from entry in entries
                let error = entry.Error
                            select RssXml.Item(
                    error.Message,
                    "An error of type " + error.Type + " occurred. " + error.Message,
                    error.Time,
                    baseUrl + "detail?id=" + Uri.EscapeDataString(entry.Id));

            var rss = RssXml.Rss(title, link, "Log of recent errors", items);

            response.Write(XmlText.StripIllegalXmlCharacters(rss.ToString()));
        }
コード例 #2
0
        public static void ProcessRequest(HttpContextBase context)
        {
            var log = ErrorLog.GetDefault(context);

            var request  = context.Request;
            var response = context.Response;

            response.ContentType = "application/xml";

            var title = string.Format(@"Daily digest of errors in {0} on {1}",
                                      log.ApplicationName, Environment.MachineName);

            var link    = ErrorLogPageFactory.GetRequestUrl(context).GetLeftPart(UriPartial.Authority) + request.ServerVariables["URL"];
            var baseUrl = new Uri(link.TrimEnd('/') + "/");

            var items = GetItems(log, baseUrl, 30, 30).Take(30);
            var rss   = RssXml.Rss(title, link, "Daily digest of application errors", items);

            context.Response.Write(XmlText.StripIllegalXmlCharacters(rss.ToString()));
        }
コード例 #3
0
ファイル: ErrorRssHandler.cs プロジェクト: syung/Elmah
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/xml";

            //
            // Get the last set of errors for this application.
            //

            const int            pageSize       = 15;
            List <ErrorLogEntry> errorEntryList = new List <ErrorLogEntry>(pageSize);
            ErrorLog             log            = ErrorLog.GetDefault(context);

            log.GetErrors(0, pageSize, errorEntryList);

            //
            // We'll be emitting RSS vesion 0.91.
            //

            RichSiteSummary rss = new RichSiteSummary();

            rss.version = "0.91";

            //
            // Set up the RSS channel.
            //

            Channel channel  = new Channel();
            string  hostName = Environment.TryGetMachineName(context);

            channel.title = "Error log of " + log.ApplicationName
                            + (hostName.Length > 0 ? " on " + hostName : null);
            channel.description = "Log of recent errors";
            channel.language    = "en";
            channel.link        = context.Request.Url.GetLeftPart(UriPartial.Authority) +
                                  context.Request.ServerVariables["URL"];

            rss.channel = channel;

            //
            // For each error, build a simple channel item. Only the title,
            // description, link and pubDate fields are populated.
            //

            channel.item = new Item[errorEntryList.Count];

            for (int index = 0; index < errorEntryList.Count; index++)
            {
                ErrorLogEntry errorEntry = (ErrorLogEntry)errorEntryList[index];
                Error         error      = errorEntry.Error;

                Item item = new Item();

                item.title       = error.Message;
                item.description = "An error of type " + error.Type + " occurred. " + error.Message;
                item.link        = channel.link + "/detail?id=" + HttpUtility.UrlEncode(errorEntry.Id);
                item.pubDate     = error.Time.ToUniversalTime().ToString("r");

                channel.item[index] = item;
            }

            //
            // Stream out the RSS XML.
            //

            context.Response.Write(XmlText.StripIllegalXmlCharacters(XmlSerializer.Serialize(rss)));
        }
コード例 #4
0
        private void Render()
        {
            Response.ContentType = "application/xml";

            ErrorLog log = ErrorLog.GetDefault(_context);

            //
            // We'll be emitting RSS vesion 0.91.
            //

            RichSiteSummary rss = new RichSiteSummary();

            rss.version = "0.91";

            //
            // Set up the RSS channel.
            //

            Channel channel  = new Channel();
            string  hostName = Environment.TryGetMachineName(_context);

            channel.title = "Daily digest of errors in "
                            + log.ApplicationName
                            + (hostName.Length > 0 ? " on " + hostName : null);
            channel.description = "Daily digest of application errors";
            channel.language    = "en";

            Uri baseUrl = new Uri(ErrorLogPageFactory.GetRequestUrl(_context).GetLeftPart(UriPartial.Authority) + Request.ServerVariables["URL"]);

            channel.link = baseUrl.ToString();

            rss.channel = channel;

            //
            // Build the channel items.
            //

            const int pageSize       = 30;
            const int maxPageLimit   = 30;
            ArrayList itemList       = new ArrayList(pageSize);
            ArrayList errorEntryList = new ArrayList(pageSize);

            //
            // Start with the first page of errors.
            //

            int pageIndex = 0;

            //
            // Initialize the running state.
            //

            DateTime       runningDay        = DateTime.MaxValue;
            int            runningErrorCount = 0;
            Item           item   = null;
            StringBuilder  sb     = new StringBuilder();
            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb));

            do
            {
                //
                // Get a logical page of recent errors and loop through them.
                //

                errorEntryList.Clear();
                log.GetErrors(pageIndex++, pageSize, errorEntryList);

                foreach (ErrorLogEntry entry in errorEntryList)
                {
                    Error    error = entry.Error;
                    DateTime time  = error.Time.ToUniversalTime();
                    DateTime day   = new DateTime(time.Year, time.Month, time.Day);

                    //
                    // If we're dealing with a new day then break out to a
                    // new channel item, finishing off the previous one.
                    //

                    if (day < runningDay)
                    {
                        if (runningErrorCount > 0)
                        {
                            RenderEnd(writer);
                            item.description = sb.ToString();
                            itemList.Add(item);
                        }

                        runningDay        = day;
                        runningErrorCount = 0;

                        if (itemList.Count == pageSize)
                        {
                            break;
                        }

                        item         = new Item();
                        item.pubDate = time.ToString("r");
                        item.title   = string.Format("Digest for {0} ({1})",
                                                     runningDay.ToString("yyyy-MM-dd"), runningDay.ToLongDateString());

                        sb.Length = 0;
                        RenderStart(writer);
                    }

                    RenderError(writer, entry, baseUrl);
                    runningErrorCount++;
                }
            }while (pageIndex < maxPageLimit && itemList.Count < pageSize && errorEntryList.Count > 0);

            if (runningErrorCount > 0)
            {
                RenderEnd(writer);
                item.description = sb.ToString();
                itemList.Add(item);
            }

            channel.item = (Item[])itemList.ToArray(typeof(Item));

            //
            // Stream out the RSS XML.
            //

            Response.Write(XmlText.StripIllegalXmlCharacters(XmlSerializer.Serialize(rss)));
        }