Posts

Showing posts with the label Escaping

Angular 2 HostListener Keypress Detect Escape Key?

Answer : Try it with a keydown or keyup event to capture the Esc key. In essence, you can replace document:keypress with document:keydown.escape : @HostListener('document:keydown.escape', ['$event']) onKeydownHandler(event: KeyboardEvent) { console.log(event); } It worked for me using the following code: const ESCAPE_KEYCODE = 27; @HostListener('document:keydown', ['$event']) onKeydownHandler(event: KeyboardEvent) { if (event.keyCode === ESCAPE_KEYCODE) { // ... } } or in shorter way: @HostListener('document:keydown.escape', ['$event']) onKeydownHandler(evt: KeyboardEvent) { // ... } Modern approach, event.key == "Escape" The old alternatives ( .keyCode and .which ) are Deprecated. @HostListener('document:keydown', ['$event']) onKeydownHandler(event: KeyboardEvent) { if (event.key === "Escape") { // Do things } }

Convert XmlDocument To String

Answer : Assuming xmlDoc is an XmlDocument object whats wrong with xmlDoc.OuterXml? return xmlDoc.OuterXml; The OuterXml property returns a string version of the xml. There aren't any quotes. It's just VS debugger. Try printing to the console or saving to a file and you'll see. As a side note: always dispose disposable objects: using (var stringWriter = new StringWriter()) using (var xmlTextWriter = XmlWriter.Create(stringWriter)) { xmlDoc.WriteTo(xmlTextWriter); xmlTextWriter.Flush(); return stringWriter.GetStringBuilder().ToString(); } If you are using Windows.Data.Xml.Dom.XmlDocument version of XmlDocument (used in UWP apps for example), you can use yourXmlDocument.GetXml() to get the XML as a string.