Skip to content

Instantly share code, notes, and snippets.

@rsgoheen
Last active August 29, 2015 14:01
Show Gist options
  • Save rsgoheen/33c38a37768722f2f811 to your computer and use it in GitHub Desktop.
Save rsgoheen/33c38a37768722f2f811 to your computer and use it in GitHub Desktop.
Linq to XML with namespaces
public static class LogQueueHelper
{
public const string exampleXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<events version=""1.2"" xmlns=""http://logging.apache.org/log4net/schemas/log4net-events-1.2"">
<event domain=""MessageDomain"" level=""INFO"" logger=""Logger"" thread=""AMQP Connection amqp-0-9://localhost:5672"" timestamp=""2014-05-08T13:16:02.6990183+01:00"" username=""rsgoheen"">
<message>LogMessage</message>
<properties>
<data name=""log4net:Identity"" value=""""/>
<data name=""log4net:UserName"" value=""rsgoheen""/>
<data name=""log4net:HostName"" value=""localhost""/>
</properties>
</event>
</events>";
public static void Main()
{
foreach (var message in GetMessages(exampleXml))
Console.WriteLine(message);
}
public static IEnumerable<string> GetMessages(string message)
{
var xdoc = XDocument.Parse(message);
foreach (var node in xdoc.Descendants(XName.Get("event", "http://logging.apache.org/log4net/schemas/log4net-events-1.2")))
{
yield return node
.Descendants(XName.Get("message", "http://logging.apache.org/log4net/schemas/log4net-events-1.2"))
.First()
.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment