[英]Create file and write xml there (C#)
我為Windows 10移動版編寫UWP應用程序。
我創建了這樣的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", "1");
el.SetAttribute("PayStateID", "0");
el.SetAttribute("Remark", "{StreetName} , ..");
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", "");
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"));
我想創建.xml文件並將此xml寫入其中。
我嘗試保存它像doc.Save("data.xml");
但有這個錯誤
Error CS1503 Argument 1: cannot convert from 'string' to 'System.IO.Stream' Murakami
我怎么能做到這一點?
非常感謝您的幫助!
由於您正在編寫Windows 10通用應用程序,因此XmlDocument.Save(string)
不可用。 相反,使用
using (FileStream fs = new FileStream("test.xml", FileMode.Create))
{
doc.Save(fs);
}
因為你正在使用Windows通用應用程序。
您需要使用以下代碼將文件保存在存儲中:
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await doc.SaveToFileAsync(file);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.