Michells Mesterværk

Irreterende load ballade med LINQ to XML

by Administrator 19. November 2009 14:35

Jeg sidder pt. og laver noget kode til at få vist nogle log filer fra en IIS. Jeg løb hertil formiddag ind i et problem, som jeg mener løsningen til, godt kunne være pakket bedre ind i .NET frameworket.

Forstil jer at skulle dette simple XML med LINQ.

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type='text/xsl' href='freb.xsl'?>
<!-- saved from url=(0014)about:internet -->
<failedRequest url="http://localhost:80/"
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="WWW Server" Guid="{3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83}"/>
<EventID>0</EventID>
<Version>1</Version>
<Level>0</Level>
<Opcode>1</Opcode>
<Keywords>0x0</Keywords>
<TimeCreated SystemTime="2009-11-15T14:12:08.325Z"/>
<Correlation ActivityID="{00000000-0000-0000-0800-0080010000FB}"/>
<Execution ProcessID="1872" ThreadID="5176"/>
<Computer>DANIELDPEDK-PC</Computer>
</System>
</Event>
</failedRequest>

I min verden er det lige ud af landevejen. Jeg ville gøre således i første hug.

XDocument xml = XDocument.Load(@"C:\Users\danielmf\Desktop\Kode\XmlDesc\XmlDesc\XmlFile1.xml", LoadOptions.None);

var q = xml.Descendants("Event").ToList();

Ovenstående virker ikke, det fandt jeg hurtigt ud. Jeg må ærligt indrømme at det tog mig noget tid at finde frem til fejlen.

For at loade elementer som har et namespace på sig er man nødt til at gøre følgende.

XDocument xml = XDocument.Load(@"C:\Users\danielmf\Desktop\Kode\XmlDesc\XmlDesc\XmlFile1.xml", LoadOptions.None);
XNamespace ns = "http://schemas.microsoft.com/win/2004/08/events/event";

var q = xml.Descendants(ns + "Event").ToList();

Om jeg synes det er hensigtsmæssigt eller elegant. Nej.

Tags:

Comments

11/19/2009 6:54:22 PM #

Mark Seemann

Hvis du sammenligner med det gamle XmlDocument eller IXPathNavigable API vil du nok synes at ovenstående er afsindigt simpelt og elegant Smile

Din første forsåg ville jo aldrig kunne virke, da et XML-dokument i princippet kunne have mange forskellige Event-elementer, der alle var definerede i forskellige namespaces...

Mark Seemann Denmark

11/20/2009 11:09:58 AM #

Daniel

@mark, det er sandt at LINQ implementationen er yderst elegant i forhold til det vi tidligere har måttet "nøjes" med.

mht. namespacet så fungere ovenstående ganske fint, også med mange Event elementer.

Daniel Denmark

2/14/2010 1:39:31 PM #

اخبار

Thank you very much for the possibility to have a look into the office. Nice boy, he has got the talent to explain.

اخبار Egypt

2/17/2010 12:30:46 PM #

Antenna

Hi..thanks for the information… loved it simply Oh man! What an amazing thing to say

Antenna United States

2/17/2010 12:31:17 PM #

Used Cell Phones

Good post....thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.

Used Cell Phones United States

2/17/2010 12:35:51 PM #

Phone Systems va

Hi… that was great stuff.. I really like reading on this subject Could you tell me more on that… I love to explore

Phone Systems va United States

2/23/2010 10:17:05 AM #

Electronics

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work

Electronics United States

2/28/2010 3:14:28 AM #

stock broker

Do you mind if I quote you on my blog if I link back to this page?

stock broker United States

2/28/2010 11:30:28 AM #

stock charts

I'm curious what CMS your website uses? It looks really good and I like all the visitor functions that are available. Sorry if this is the wrong place to ask this but I wasn't sure how to contact you - thanks.

stock charts United States

3/4/2010 6:21:47 AM #

watch movies online

I enjoy watching movies online, it is way cheaper than going to the theaters.

watch movies online United States

3/9/2010 8:42:10 PM #

WoW Mobile

Do you know which mobile phone companies allow tethering?

WoW Mobile United States

3/10/2010 12:00:27 AM #

WoW Mobile

Do you know which mobile phone companies allow tethering?

WoW Mobile United States

3/14/2010 11:25:12 PM #

ssk sorgulama

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

ssk sorgulama United States

3/20/2010 3:23:49 PM #

ehliyet

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

ehliyet Denmark

3/28/2010 3:25:27 PM #

Clayton Natural Health

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

Clayton Natural Health United States

4/6/2010 8:00:24 PM #

horde leveling guide

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

horde leveling guide United States

4/7/2010 8:03:24 PM #

canon ef 50mm f/1.8 ii camera lens

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

canon ef 50mm f/1.8 ii camera lens United States

4/7/2010 10:31:14 PM #

coffee roasters

Super-Duper website! I am loving it!! Be back again - receiving your feeds as well, Thanks.

coffee roasters United States

Add comment


(Will show your Gravatar icon)

(De 3 specielle karaktere i det danske alfabet?)
  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen