Autor Beitrag
ebber
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 239
Erhaltene Danke: 1

Win XP, Win 7
C# (VS 2010), Delphi (2007), Expression 4
BeitragVerfasst: Di 28.10.08 18:27 
Hallo,

ist ja bestimmt ganz einfach, aber ich komme einfach nicht dahinter, wie ich eine XML Node erstelle.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
            XmlDocument xmlD = new XmlDocument();
            XmlNode xmlN;
            XmlNode newXmlN;

            xmlD.Load("numDBBB.xml");
            xmlN = xmlDoc.ChildNodes[1];
            xmlN.RemoveAll();

            xmlN.AppendChild(newXmlN);


Also newXmlN will ich erstellen und dann als Child an xmlN machen. Aber wie gebe ich der newXmlN jetzt einen Namen und Attribute ?

MfG
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Di 28.10.08 22:17 
XmlNode lässt sich als abstrakte Klasse schlecht instanzieren, was du vielleicht schon selbst bemerkt hast ;) . Als Ableitung gibt es für Elemente z.B. XmlElement, das auch eine Eigenschaft für Attribute besitzt.

PS: Ab 3.5 ist Linq to XML dem XmlDocument vorzuziehen.

_________________
>λ=
ebber Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 239
Erhaltene Danke: 1

Win XP, Win 7
C# (VS 2010), Delphi (2007), Expression 4
BeitragVerfasst: Mi 29.10.08 00:17 
Also so funktioniert das jetzt, Danke.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
            XmlDocument xmlD = new XmlDocument();
            XmlNode xmlN;
            XmlElement elem = xmlD.CreateElement("num");
            
            elem.SetAttribute("ebber""n89");

            xmlD.Load("numDBBB.xml");
            xmlN = xmlD.ChildNodes[1];
            xmlN.RemoveAll();

            xmlN.AppendChild(elem);


Ich hatte vorher schon ein wenig damit rumprobiert, aber da war der Fehler dann an einer andren Stelle: xmlDoc... von oben war ein anderes XmlDocument.

Das mit Linq wäre vermutlich besser, aber ich habe mich bis jetzt noch nicht damit befasst, habe aber vor das sehr bald zu tun.

MfG