
Ergebnis 1 bis 2 von 2
-
Bin neu hier
- 27.11.2010, 11:31
- #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; } }
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(); } } }
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.
Was mache ich falsch?
Ich danke euch schon mal.
Gruß,
Nico
-
Gehöre zum Inventar
- 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.
Ähnliche Themen
-
Kontakte von .csv-Datei in Kies importieren, Problem mit Nr.-Typen
Von maxi_mus im Forum Samsung Galaxy SAntworten: 7Letzter Beitrag: 10.09.2010, 10:26 -
cab-Datei/_setup.xml bearbeiten?
Von idephili im Forum HTC HD2 InterfaceAntworten: 4Letzter Beitrag: 30.05.2010, 15:53 -
Datei-Explorer Problem
Von Kampftier im Forum Touch HD AnwendungsprogrammeAntworten: 3Letzter Beitrag: 01.02.2010, 17:33 -
XML Datei installieren ?
Von Unregistriert im Forum HTC Touch Pro 2Antworten: 5Letzter Beitrag: 26.08.2009, 12:59 -
Datei Explorer Problem
Von drmib im Forum Touch HD AnwendungsprogrammeAntworten: 5Letzter Beitrag: 29.11.2008, 10:30
Pixel 10 Serie mit Problemen:...