2015-12-06 5 views
0

Я пишу Android приложение на C# (Xamarin)Невозможно записать XML в файл

Я пытаюсь написать XML в файл

код создания XML

XmlDocument doc = new XmlDocument(); 
      XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("Order")); 
      el.SetAttribute("CallConfirm", "1"); 
      el.SetAttribute("PayMethod", "Безнал"); 
      el.SetAttribute("QtyPerson", ""); 
      el.SetAttribute("Type", "2"); 
      el.SetAttribute("PayStateID", "0"); 
     el.SetAttribute("Remark", "{Comment}"); 
      el.SetAttribute("RemarkMoney", "0"); 
      el.SetAttribute("TimePlan", ""); 
      el.SetAttribute("Brand", "1"); 
      el.SetAttribute("DiscountPercent", "0"); 
      el.SetAttribute("BonusAmount", "0"); 
      el.SetAttribute("Department", ""); 

      XmlElement el2 = (XmlElement)el.AppendChild(doc.CreateElement("Customer")); 

      el2.SetAttribute("Login", ""); 
     el2.SetAttribute("FIO", "{FIO}"); 

      XmlElement el3 = (XmlElement)el.AppendChild(doc.CreateElement("Address")); 

     el3.SetAttribute("CityName", "{CityName}"); 
      el3.SetAttribute("StationName", ""); 
     el3.SetAttribute("StreetName", "{StreetName}"); 
     el3.SetAttribute("House", "{HouseName}"); 
      el3.SetAttribute("Corpus", ""); 
      el3.SetAttribute("Building", ""); 
     el3.SetAttribute("Flat", "{FlatName}"); 
      el3.SetAttribute("Porch", ""); 
      el3.SetAttribute("Floor", ""); 
      el3.SetAttribute("DoorCode", ""); 

      XmlElement el4 = (XmlElement)el.AppendChild(doc.CreateElement("Phone")); 

     el4.SetAttribute("Code", "{Code}"); 
     el4.SetAttribute("Number", "{Phone}"); 

     XmlElement el5 = (XmlElement)el.AppendChild(doc.CreateElement("Products")); 
     XmlElement el6 = (XmlElement)el5.AppendChild(doc.CreateElement("Product")); 

     el6.SetAttribute("Code", "{ProductCode}"); 
     el6.SetAttribute("Qty", "{QTY}"); 


      Console.WriteLine ("TUT"); 
      Console.WriteLine(doc.OuterXml); 

Я пытаюсь написать это , но я не вижу его на устройстве.

var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
var filePath = Path.Combine(documentsPath, "myFile.xml"); 
File.WriteAllText(filePath, doc.ToString()); 

Как записать это в файл или, может быть, что-то не в моем коде?

ответ

0

В строке File.WriteAllText(filePath, doc.ToString());doc.ToString()System.Xml.XmlDocument.

Вместо этого используйте doc.OuterXml или doc.Save(path) метод.

+0

Где я могу видеть файл с xml? –

+0

Из вашего кода значение переменной 'filePath' равно« PATH_TO_USERS_FOLDER/CURRENT_USER/Documents/myFile.xml ». Как «C: \ Users \ User \ myFile.xml» (в окнах). Он может быть в папке документа текущего пользователя (In android). –

+0

Но я пишу для телефона Android, а не ПК –

Смежные вопросы