problem mit XML Datei problem mit XML Datei
Danke Danke:  0
Ergebnis 1 bis 2 von 2
  1. Hi,

    Hoffe ihr könnt mir bei einem Problem helfen.
    In meinem App was ich derzeit entwickle erstelle ich eine XML Datei und schreibe Daten in dies.
    Dazu habe ich folgende Funktionen geschrieben:
    Code:
            public static void CreateCarsDB(string strXMLFile)
            {
                try
                {
                    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        XElement _item = new XElement("Cars");
                        _item.Add();
                        XDocument _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), _item);
    
                        using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(strXMLFile, FileMode.Create, storage))
                        {
                            StreamWriter file = new StreamWriter(location);
                            _doc.Save(file);
                            file.Dispose();
                            location.Dispose();
                        }
                    }
                }
    
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
    
            public static int SaveCar(Car car, string strCarDB_Path)
            {
                int ActID = 0;
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(strCarDB_Path, FileMode.Open, storage))
                    {
                        XDocument data = XDocument.Load(location);
                        if (car.ID > 0)
                        {
                            XElement carElement = data.Descendants("Car").Where(c =>
                                c.Attribute("ID").Value.Equals(car.ID.ToString())).FirstOrDefault();
                            if (carElement != null)
                            {
                                carElement.SetElementValue("CarReg", car.CarReg);
                                carElement.SetElementValue("CarType", car.CarType);
                                carElement.SetElementValue("Mileage", car.Mileage);
                                carElement.SetElementValue("CunstYear", car.CunstYear);
                                carElement.SetElementValue("NextTUV", car.NextTUV);
                                carElement.SetElementValue("NextServiceDate", car.NextServiceDate);
                                carElement.SetElementValue("NextServiceKM", car.NextServiceKM);
                                carElement.SetElementValue("FuelType", car.FuelType);
    
                                StreamWriter file = new StreamWriter(location);
                                data.Save(file);
                                file.Dispose();
                                location.Dispose();
                                ActID = car.ID;
                            }
                        }
                        else
                        {
                            XElement newCar = new XElement("Car",
                                new XElement("CarReg", car.CarReg),
                                new XElement("CarType", car.CarType),
                                new XElement("Mileage", car.Mileage),
                                new XElement("CunstYear", car.CunstYear),
                                new XElement("NextTUV", car.NextTUV),
                                new XElement("NextServiceDate", car.NextServiceDate),
                                new XElement("NextServiceKM", car.NextServiceKM),
                                new XElement("FuelType", car.FuelType));
    
                            ActID = GetNextAvailableID(data);
    
                            newCar.SetAttributeValue("ID", ActID);
                            data.Element("Cars").Add(newCar);
                            
                            StreamWriter file = new StreamWriter(location);
                            data.Save(file);
                            file.Dispose();
                            location.Dispose();
    
                        }
                    }
                    
                }
                return ActID;
            }
    
            private static int GetNextAvailableID(XDocument _xdoc)
            {
                try
                {
                    /*using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(strCarDB_Path, FileMode.Open, storage))
                        {*/
                            //XDocument data = XDocument.Load(test_loc);
    
                            return Convert.ToInt32(
                                    (from c in _xdoc.Descendants("Car")
                                     orderby Convert.ToInt32(c.Attribute("ID").Value) descending
                                     select c.Attribute("ID").Value).FirstOrDefault()
                                    ) + 1;
                        //}
    
                    //}
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                    return -1;
                }
            }
    Diese scheint soweit auch zu funktionieren. Leider kann ich mir das im IsolateStorage erstellte file ja nciht anschauen.

    Nun versuche ich mit folgender Funktion Daten aus der XML zu lesen:
    Code:
            public static Car GetCar(string strCarDB_Path, int carID)
            {
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(strCarDB_Path, FileMode.Open, storage))
                    {                    
                        XDocument data = XDocument.Load(location);
    
                        return (from c in data.Descendants("Car")
                                where c.Attribute("ID").Value.Equals(carID.ToString())
                                select new Car()
                                {
                                    ID = Convert.ToInt32(c.Attribute("ID").Value),
                                    CarReg = c.Element("CarReg").Value,
                                    CarType = c.Element("CarType").Value,
                                    Mileage = c.Element("Mileage").Value,
                                    NextTUV = Convert.ToDateTime(c.Element("NextTUV").Value),
                                    NextServiceDate = Convert.ToDateTime(c.Element("NextServiceDate").Value),
                                    NextServiceKM = Convert.ToInt32(c.Element("NextServiceKM").Value),
                                    FuelType = c.Element("FuelType").Value
                                }).FirstOrDefault();
    
                    }
                }
            }
    und genau hier ist das Problem. Wenn ich die Funktion GetCar() aufrufe kommt es in der Zeile "XDocument data = XDocument.Load(location);" zu einer Exception mit der folgenden Message:

    Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 11.
    Leider kann ich keinen Fehler finden.
    Was mache ich falsch?

    Ich danke euch schon mal.

    Gruß,
    Nico
    0
     

  2. 29.11.2010, 15:29
    #2
    Ohne mir das jetzt im Detail angesehen zu haben:

    1.) Du debuggst das nicht auf nem Phone sondern im Emulator?
    2.) Wann werden die Funktionen aufgerufen, der Emulator behält die Daten nämlich nicht wenn die Anwendung neu vom Studio aus deployt wird, d.h. wenn ich vom Studio aus starte, das File schreibe und dann wieder vom Studio aus starte (wo neu deployt wird) und laden will, dann ist einfach das File schon nicht da.

    Alternativ könntest du auch einfach den Filestream in einen String lesen und in einer Textbox anzeigen -dann siehst du evtl. wo der Fehler im XML wäre.
    0
     

Ähnliche Themen

  1. Antworten: 7
    Letzter Beitrag: 10.09.2010, 10:26
  2. cab-Datei/_setup.xml bearbeiten?
    Von idephili im Forum HTC HD2 Interface
    Antworten: 4
    Letzter Beitrag: 30.05.2010, 15:53
  3. Datei-Explorer Problem
    Von Kampftier im Forum Touch HD Anwendungsprogramme
    Antworten: 3
    Letzter Beitrag: 01.02.2010, 17:33
  4. XML Datei installieren ?
    Von Unregistriert im Forum HTC Touch Pro 2
    Antworten: 5
    Letzter Beitrag: 26.08.2009, 12:59
  5. Datei Explorer Problem
    Von drmib im Forum Touch HD Anwendungsprogramme
    Antworten: 5
    Letzter Beitrag: 29.11.2008, 10:30

Besucher haben diese Seite mit folgenden Suchbegriffen gefunden:

windows phone 7 xml

windows phone xml

xml windows phone 7

windows phone 7 xml auslesen

Xelement umlaute

windows phone entwicklung save data

WIndows Phone 7 xml schreiben

wp7 xml auslesen

windows phone 7 xml-datei

XML einlesen Wp7

XDocument Umlaute

xml dateien wp7

wp7 save data xml

c# wp7 xml

wp7 xml id

wp7 c# xml speichern

windows phone 7 xml laden

xml datei lesen umlaute windows phone

xdocument windows phone

wp7 c# xml auslesenxml wp7 umlautexelement setelementvalue funktioniert nichtxml datei import in isolated storageisolatedstoragefile csvwindows phone c# xdocument using

Stichworte