-3

I want to search through my xml file. The structure looks like this:

<AForetag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Place ID="1006">
        <Foretag>
            <Epost>info@abc123.se</Epost>
            <Namn>Abe</Namn>
            <Ort>Abc123</Ort>
            <Adress>Abc123</Adress>
            <Postnummer>Abc123</Postnummer>
            <Landskap>Abc123</Landskap>
            <Telefon>Abc123</Telefon>
            <Medlemskap>Abc123</Medlemskap>
        </Foretag>
        <Foretag>
            <Epost>def456</Epost>
            <Namn>def456</Namn>
            <Ort>def456</Ort>
            <Adress>def456</Adress>
            <Postnummer>def456</Postnummer>
            <Landskap>def456</Landskap>
            <Telefon>def456</Telefon>
            <Medlemskap>def456</Medlemskap>
        </Foretag>
    </Place>
</Aforetag>

And I want to search for the Element <Landskap>. And if I get and match I should pick all the other elements, Epost, Namn, Ort, Adress, Postnummer, Landskap, Telefon and Medlemskap. The info I want to put in an array.

I have tried this:

var aforetag = from foretag in doc.Descendants("Place")
                       where foretag.Attribute("ID").Value == "1006"
                       select foretag;


var landskap = aforetag.Elements("Foretag")
                       .Descendants()
                       .Where(x => x.Element("Landskap")
                       .Value
                       .Contains(s)
                       .Descendants()
                       .Select(c => (string)c)
                       .ToArray();
Steve B
  • 36,818
  • 21
  • 101
  • 174
faceplant
  • 1
  • 1
  • 2
    You tried that. And, what happened? – John Saunders Oct 15 '13 at 08:40
  • Ill get this Warning: NullReferenceException was unhandled. Objectreferens has not been given an instance of an object. (Sorry for bad translate) And its stops to work. – faceplant Oct 15 '13 at 08:43
  • 2
    So, you also don't know what an exception is. Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 15 '13 at 08:52
  • Got it to work. Thanks, its my XML schema thats was messed up. Had some bad values in it like '--'. – faceplant Oct 15 '13 at 09:07
  • @faceplant your query is not good, I doubt it doesn't work as you expected unless you have had some modification to make it work – King King Oct 15 '13 at 09:40

2 Answers2

0
var landskap = aforetag.Elements("Foretag")
                       .Where(e=>e.Element("Landskap").Value.Contains(s))
                       .Select(e=>e.Elements().Select(x=>x.Value).ToArray());
//the result is an IEnumerable<string[]> for the matched keyword s
King King
  • 61,710
  • 16
  • 105
  • 130
0

Your code is not well formed. Copy this into VS and there are a few errors, fix one and more errors!...

And most importantly, your XML is not XML as the start and end tag don't even match! Plus, there are other issues.

Fix all these and I'm sure it will help.

Dave
  • 8,163
  • 11
  • 67
  • 103