LinqToXML Code läuft in C# aber nicht in VB.NET

multitasker

Mitglied
Hallo,
ich habe folgende Code von C# in VB.NET übernommen. Erhalte jedoch in der Zeile mit dem STringvergleich einen Fehler, dass der Objektverweis nicht auf eine Objektinstanz festgelegt wurde.

Code:
            Dim queryVariable = From item In XDoc.Descendants("Apartment").Descendants("Variable").Attributes("TypeID") _
                                Select New With { _
                                    .Typnummer = item.Value, _
                                    .Datentyp = (From element In XDoc.Descendants("Apartment").Descendants("Type") _
                                                    Where element.Attribute("TypeID").Value.ToString = item.Value.ToString _
                                                    Select element.Value).First()}

Der Code in C# funktioniert:
Code:
            var queryVariable = from item in doc.Descendants("Apartment").Descendants("Variable").Attributes("TypeID")
                                select new
                                {
                                    Typnummer = item.Value,
                                    Datentyp = (from element in doc.Descendants("Apartment").Descendants("Type")
                                                where (string) element.Attribute("TypeID") == (string)item.Value
                                                select element.Value).First()
                                };


Weiß jmd. woran dies liegen könnte?

Gruß
 
Zuletzt bearbeitet:
Hi

In C# castest du nach string und in VB.NET konvertierst du nach Int32. Ich glaub da könnte der Hund begraben liegen.
 
Hallo Nico,
sorry, habe mehrere Dinge versucht und beim Einstellen hier ins Forum nicht alles rückgängig gemacht.

In dem Element element.Attribute("TypeID").Value befindet sich ein Integerwert. Deshalb habe ich danach noch per KOnvertierung versucht es zum Laufen bekommen.

Die Zeile soll natürlich so aussehen und werde es gleich ändern im ersten Posting (falls es geht). Bei dem Code hätte ja auch der Compiler sich beschwert.

Code:
Where element.Attribute("TypeID").Value.ToString = item.Value.ToString _
 
Zuletzt bearbeitet:
Hi.

Wie sieht denn dein Dokument aus.

Ich hab deine Abfragen mal schnell mit LINQPad ausprobiert und die gleichen Ergebnisse bekommen.

Vermutlich gibt es ein Type Element welches keine TypeID besitzt?

Gruß
 
Es handelt sich um eine XML Datei. Kann ich diese auch mit LinqPad abfragen?
Natürlich.
Code:
Dim doc = XDocument.Load("c:\pfad\zur\datei\1_Bild_mit_2_Elementen.XML")
doc.Dump("XDocument")
Wie bereits vermutet, die Ausgabe von
C#:
var q = from i in doc.Descendants("Apartment").Descendants("Type") select i.Attribute("TypeID");
	q.Dump("q");
ist für das fragliche XML:
Code:
null  
TypeID="6"
D.h. es gibt ein Type Element welches kein TypeID Attribut enthält.

Wenn du den VB.NET Code so änderst:
Code:
where element.Attribute("TypeID") = item.Value
dann bekommst du das gleiche Ergebnis wie bei C#.

Gruß
 
Ich habe es so versanden:

Code:
            Dim queryVariable = From item In XDoc.Descendants("Apartment").Descendants("Variable").Attributes("TypeID") _
                                Select New With { _
                                    .Typnummer = item.Value, _
                                    .Datentyp = (From element In XDoc.Descendants("Apartment").Descendants("Type") _
                                                    Where element.Attribute("TypeID") = item.Value _
                                                    Select element.Value).First()}

jedoch bekomme ich so natürlich schon beim Kompilieren die Meldung, dass XElement nicht in STring konvertiert werden kann.
 
Ich habe es so versanden:

Code:
            Dim queryVariable = From item In XDoc.Descendants("Apartment").Descendants("Variable").Attributes("TypeID") _
                                Select New With { _
                                    .Typnummer = item.Value, _
                                    .Datentyp = (From element In XDoc.Descendants("Apartment").Descendants("Type") _
                                                    Where element.Attribute("TypeID") = item.Value _
                                                    Select element.Value).First()}

jedoch bekomme ich so natürlich schon beim Kompilieren die Meldung, dass XElement nicht in STring konvertiert werden kann.
OK. Am saubersten wäre es sowieso den Fehlerfall abzufangen:
Code:
From element In XDoc.Descendants("Apartment").Descendants("Type") _
	Let type = element.Attribute("TypeID") _
	Where type IsNot Nothing and type.Value = item.Value
Gruß

\edit: Noch schöner ist es vermutlich mit einem join:
Code:
From item _
In XDoc.Descendants("Apartment").Descendants("Variable").Attributes("TypeID") _
join e _
in XDoc.Descendants("Apartment").Descendants("Type") _
       .Where(Function(e) e.Attribute("TypeID") isNot Nothing) _
on e.Attribute("TypeID").Value equals item.Value _
select new with { .Typnummer = item.Value, .Datentyp = e.Value }
 
Zuletzt bearbeitet:
Danke für deine tatreiche Unterstütung. Leider funktioniert es bei mir immer noch nicht. Weder im VS noch im LinqPad.
 

Anhänge

  • Fehlermeldung_Linq.PNG
    Fehlermeldung_Linq.PNG
    46,9 KB · Aufrufe: 23
  • Fehlermeldung_Linq2.PNG
    Fehlermeldung_Linq2.PNG
    15,3 KB · Aufrufe: 21
Zurück