Autor Beitrag
alegria
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Sa 29.01.11 11:49 
Hallo,

ist es möglich ohne direkte umständliche bzw. code und zeitaufwändige Zuordnung möglich, aus einem XELement direkt in eine bestimmte Klasse zu "konvertieren"?

Also bisher hab ich sowas:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
class Person
{
string Vorname {get; set;}
string Nachname {get; set;}
}

List<Person) pLst = new PErson();
var abc = XElement.Load(myXMLfile).Elements();
foreach (item in abc)
  pLst.Add(new Person{Vorname = item.Element("Vorname"), Nachname = item.Element("Nachname")});


Was ich möchte wäre sowas wie "direkt" "pLst.Add(new Person{item});" weil die XML-Nodes vom Namen her identisch mit den Properties der Klasse Person sind...

Geht sowas???
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: Sa 29.01.11 12:05 
schau dir mal das Theama "XML-Serialisierung" an. müsste sein was du suchst...
www.c-sharp-library....und+laden_64947.html
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Sa 29.01.11 13:07 
Hallo,
wenn du für das Xml ein Schema hast (oder eins generieren tust) kannst du dir Klassen dafür generieren lassen. Schau mal nach XSD.
Gruß
alegria Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Sa 29.01.11 18:50 
:) und :(

Also XMLSerializer scheint schonmal definitiv die richtige Ecke zu sein...

Aber ich komme nicht so richtig weiter da ich ein "root" Node um die einschliessenden eigentlichen Infos habe...

<root>
<person>
<vorname>hans</vorname>
</person>
<person>
<vorname>otto</vorname>
<person>
</root>

Mittels:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
XmlSerializer xs = new XmlSerializer(typeof(root));
root r = (root)xs.Deserialize(new XmlTextReader("../../XMLFile2.xml"));
r.ToString();

    public class root {
        public person person;
    }

    public class person
    {
        public string name { get; set; }
        public string rating { get; set; }
    }

komme ich schonmal nach ans gewünschte Ergebnis, aber ich erhalte
a) nur den ersten Datensatz und
b) habe ich ein problem beim auslesen der "adresse" (andere klasse) die ja auch wieder von root umschlossen ist...

Gibts denn nicht irgendwie die möglichkeit erst bei nodelevel2 zu beginnen???

@danielf: namespace gibts leider keines...
alegria Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Sa 29.01.11 19:04 
Juhu! :)
ausblenden C#-Quelltext
1:
2:
XmlSerializer xs = new XmlSerializer(typeof(List<person>),new XmlRootAttribute("root"));
List<person> lar = (List<person>)xs.Deserialize(new XmlTextReader("../../XMLFile2.xml"));

Damit wäre das dann wohl gelöst...