Autor Beitrag
Chiyoko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: So 13.06.10 13:56 
Huhu,


ich habe versucht, nodes an ein bestehendes XDocument anzufuegen aber
i-wie klapppt das nicht so, wie es ueberall steht.
Ich denke, ich habe ein detail uebersehen?
(Das anlegen funktioniert tadellos, es geht nur ums anfuegen)
Danke.

Mein Ansatz:

Ich habe mnehrere Anlaeufe getestet, aber immer gab es einen Error oder die Datei wurde erst gar nicht geoeffnet.
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:
DateTime aktuelleZeit = DateTime.Now;

IPHostEntry iphostentry = Dns.GetHostEntry(Dns.GetHostName());

if (aktuelleZeit.ToLongTimeString() == "13:50:00")
{
    if (File.Exists(Path.Combine(Application.StartupPath, @"IPadressen.xml")))
    {
        XDocument doc = XDocument.Load(Path.Combine(Application.StartupPath ("IPadressen.xml")));
        //var IpAdressen = doc.XPathSelectElements("IP_Adressen");

        doc.Element("IP_Adressen").AddAfterSelf(new XElement("IPs",
               new XAttribute("Time", DateTime.Now.ToString("t")),
               new XAttribute("Date", DateTime.Now.ToString("d")),
               new XElement("IP""IPtest")));
                       
    }
    else
    {
        foreach (IPAddress ipaddress in iphostentry.AddressList)
        {
            XDocument docConfig = new XDocument(
                new XComment("IP_Adressen_auflisten"),
                new XElement("IP_Adressen",
                new XElement("IPs",
                  new XAttribute("Time", DateTime.Now.ToString("t")),
                  new XAttribute("Date", DateTime.Now.ToString("d")),
                  new XElement("IP", ipaddress))));

            docConfig.Save(Path.Combine(Application.StartupPath, @"IPadressen.xml"), SaveOptions.None);
        }
    }
}
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 13.06.10 14:00 
Du gibst uns weder die Exception noch die Dokumentstruktur, das ist nicht gerade viel. Aber wo speicherst du im if-Block das Dokument wieder ab?

_________________
>λ=
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: So 13.06.10 14:07 
Ah ich Honk-.-...
Ich habe vergessen, save anzugeben.,..
Danke, das funktioniert. Und ja, naechstes mal gebe ich die Xml Struktur mit an,...ich dachte nur, das dies schon vom Code her ersichtlich sei.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 13.06.10 14:27 
Hab erst nach dem Absenden bemerkt, dass im else-Teil die Datei neu erzeugt wird - was daran liegen könnte, das dort auch noch ein Fehler steckt ;) . Denk nochmal drüber nach: Du erstellst n Dokumente und speicherst sie alle unter dem gleichen Namen.

_________________
>λ=
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: So 13.06.10 14:35 
Naja, da koenntest du recht haben...

Die Datei erstellt an die 10 x die selben Nodes.Obwohl ja nur einmal erstellt werden soll.
Dann dacht ich mir, ok stellste den Timer auf 1000.
Nun wird aber immer noch 2 x der selbe Node erstellt.

Ansonsten:
Der komplette Code befindet sich im Timer.

Aber "else" muesste doch richtig sein?...Wenn die Datei nicht exestiert, erstell neu.
Oder an was hast du da gedacht?

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:
private void timer1_Tick(object sender, EventArgs e)
        {
            Worldtimer(sender, e);
            timer1.Interval = 1000;      
            DateTime aktuelleZeit = DateTime.Now;

            //MessageBox.Show(new System.Net.WebClient().DownloadString("http://myip.bdsw.de"));
            IPHostEntry iphostentry = Dns.GetHostEntry(Dns.GetHostName());

            if (aktuelleZeit.ToLongTimeString() == "14:26:00")
            {
                if (File.Exists(Path.Combine(Application.StartupPath, @"IPadressen.xml")))
                {
                    XDocument doc = XDocument.Load(Path.Combine(Application.StartupPath, ("IPadressen.xml")));

                    foreach (IPAddress ipaddress in iphostentry.AddressList)
                    {
                        doc.Element("IP_Adressen").Add(new XElement("IPs",
                                new XAttribute("Time", DateTime.Now.ToString("t")),
                                new XAttribute("Date", DateTime.Now.ToString("d")),
                                new XElement("IP", ipaddress)));
                        doc.Save(Path.Combine(Application.StartupPath, @"IPadressen.xml"), SaveOptions.None);
                    }
                }
                else
                {
                    foreach (IPAddress ipaddress in iphostentry.AddressList)
                    {
                        XDocument docConfig = new XDocument(
                            new XComment("IP_Adressen_auflisten"),
                            new XElement("IP_Adressen",
                            new XElement("IPs",
                              new XAttribute("Time", DateTime.Now.ToString("t")),
                              new XAttribute("Date", DateTime.Now.ToString("d")),
                              new XElement("IP", ipaddress))));

                        docConfig.Save(Path.Combine(Application.StartupPath, @"IPadressen.xml"), SaveOptions.None);
                    }
                }
            }
        }
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 13.06.10 15:04 
user profile iconChiyoko hat folgendes geschrieben Zum zitierten Posting springen:
Naja, da koenntest du recht haben...

Die Datei erstellt an die 10 x die selben Nodes.Obwohl ja nur einmal erstellt werden soll.
Nein, das Problem des else-Teils ist, dass er zu wenige Nodes speichert, nämlich nur die letzte IP. Auch im if-Teil speicherst du auf einmal für jede Adresse einmal, reicht denn einmal nicht :gruebel: ?

user profile iconChiyoko hat folgendes geschrieben Zum zitierten Posting springen:
Nun wird aber immer noch 2 x der selbe Node erstellt.
In zwei verschiedenen Timer-Aufrufen? Ich dachte, das wäre das Ziel?

_________________
>λ=
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: So 13.06.10 15:19 
Oehm hehe...

Nein, das einzige was ich erreichen moechte ist :

-> Systemzeit auslesen, wenn Systemzeit == angegebene Zeit, dann schreibe die current IP in die Xml datei.
Und zwar automatisch immer um die selbe Zeit, jeden Tag.


Btw das ist die Ausgabe.

Und da soll eigentlich nur jeweils eine angehaengt werden.
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
<?xml version="1.0" encoding="utf-8"?>
<!--IP_Adressen_auflisten-->
<IP_Adressen>
  <IPs Time="15:29" Date="13.06.2010">
    <IP>192.168.178.20</IP>
  </IPs>
  <IPs Time="15:29" Date="13.06.2010">
    <IP>192.168.178.20</IP>
  </IPs>
</IP_Adressen>


Zitat:

Nein, das Problem des else-Teils ist, dass er zu wenige Nodes speichert, nämlich nur die letzte IP. Auch im if-Teil speicherst du auf einmal für jede Adresse einmal, reicht denn einmal nicht :gruebel: ?


Eigentlich funktiert das wunderbar und es werden auch immer beide gespeichert.
Alte vorhandene IP + neue.,...aber halt nur doppelt...ich weis nicht warum...


Na war ja klar, liegt "wiedermal" am Timer....i-wie komm ich damit noch nicht wirklich klar.
Pack ich das in eine klickmethode , funktioniert alles fehlerfrei.
Ich probier mal bissl weiter.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 13.06.10 18:03 
Auch eine Möglichkeit, durch ein foreach die Leser komplett in die Irre zu führen, wenns ein AdressList[0] getan hätte ;) .

_________________
>λ=
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: So 13.06.10 19:53 
Ja sorry, ich versuch mich damit zum ersten mal^^
ich habe keine Ahnung von IP s im Net Framework.

Aber vom Array hab ich auch schon gelesen, nur war mir foreach lieber:D
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: So 13.06.10 21:55 
Das einige Problem, was ich noch habe, ist:

Der Knoten legt sich 2 mal an....fragt sich jetzt:Warum?
Wenn der Timer doch auf genau 1000 ms steht und angeblich unabhaengig von der CPU ist.

oder geh ich das falsch an?


EDIT:

Und problem geloest:D

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
private void timer1_Tick(object sender, EventArgs e)
{
     timer1.Interval = 1000;      // die Zeit jede Sekunde aktualisieren 
     
     if (Label_time.Text == "22:34:00")
     {
         Thread.Sleep(1000);
         toolStripMenuItem_IP.PerformClick();
     }
}