Autor Beitrag
D.Follmann
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: Mi 05.09.07 07:45 
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:
ausblenden 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:

ausblenden volle Höhe C#-Quelltext
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);                 //Datei speichern
            xd.Save(Console.Out);  //XML auf Console ausgeben
            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!


Zuletzt bearbeitet von D.Follmann am Do 06.09.07 10:06, insgesamt 1-mal bearbeitet
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Mi 05.09.07 12:33 
Schau Dir mal den XmlSerializer an, der dürfte Dir in der Hinsicht auch sehr gut helfen können. Damit kannst Du Objekte und ganze Strukturen in XML persistieren. Isch hab da och ma was vorbereided ;): www.c-sharp-library....und+laden_64947.html