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:
Diese scheint soweit auch zu funktionieren. Leider kann ich mir das im IsolateStorage erstellte file ja nciht anschauen.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:
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: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();
}
}
}
Leider kann ich keinen Fehler finden.Zitat:
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