verwendetes Datenbanksystem: <XML>
Hallo zusammen,
ich mache grade die ersten Versuche mit XML-Lesen/Screiben in C#. Ich möchte auf jeden Fall die DOM-Struktur nutzen.
Folgende Übung habe ich gemacht:
ich wollte ein bestimmtes vorgegebenes XML-Dokument in DOM nachbilden und dann auf die Festplatte schreiben.
Hier die XML-Daten:
XML-Daten
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
| <?xml version="1.0" standalone="yes"?> <books> <book> <author>Carson</author> <price format="dollar">31.95</price> <pubdate>05.01.2001</pubdate> </book> <pubinfo> <publisher>MSPress</publisher> <state>WA</state> </pubinfo> </books> |
Und ich habe das wie folgt gelöst:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
| using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO;
namespace XML_Test { class Xml_Test { static void Main(string[] args) { FileStream fs = new FileStream("C:\\testXML.xml", FileMode.Create); XmlTextReader xr = new XmlTextReader(fs); XmlDocument xd = new XmlDocument();
xd.AppendChild(xd.CreateXmlDeclaration("1.0", String.Empty, "yes")); xd.AppendChild(xd.CreateElement("books")); XmlNode root = xd.DocumentElement;
XmlNode x = root.AppendChild(xd.CreateElement("book")); XmlElement y = (XmlElement) x.AppendChild(xd.CreateElement("author")); y.InnerText = "Carson"; y = (XmlElement) x.AppendChild(xd.CreateElement("price")); y.InnerText="31.95"; y.SetAttribute("format", "dollar"); y = (XmlElement)x.AppendChild(xd.CreateElement("pubdate")); y.InnerText = "05.01.2001"; x = root.AppendChild(xd.CreateElement("pubinfo")); y = (XmlElement)x.AppendChild(xd.CreateElement("publisher")); y.InnerText = "MSPress"; y = (XmlElement)x.AppendChild(xd.CreateElement("state")); y.InnerText = "WA";
xd.Save(fs); xd.Save(Console.Out); fs.Close();
} } } |
Das klappt auch und ich erhalte ein testXML.xml mit genau den gewünschten Daten. Ist das der richtige Weg um so etwas zu machen, oder habe ich es mir im Programm da zu kompliziert gemacht?!?
Vielen Dank!