3

I writing UWP app for windows 10 mobile.

I created xml like this:

 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"));

I want to create .xml file and write this xml to it.

I try save it like this doc.Save("data.xml"); but have this error

Error   CS1503  Argument 1: cannot convert from 'string' to 'System.IO.Stream'  Murakami    

How I can make this?

Thank's very much for help!

leppie
  • 115,091
  • 17
  • 196
  • 297
  • 1
    Did you try searching how to save an `XmlDocument`? You'd get a lot of results! – Charles Mager May 06 '16 at 06:52
  • 1
    Possible duplicate of [How to create XML document using XmlDocument?](http://stackoverflow.com/questions/11492705/how-to-create-xml-document-using-xmldocument) – Charles Mager May 06 '16 at 06:52
  • @CharlesMager it's an Universal App – Adriano Repetti May 06 '16 at 07:08
  • @ЕвгенийСухомлин there are some posts here on SO about this, I didn't find an exact dupe then I just point you to an example: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XmlDocument – Adriano Repetti May 06 '16 at 07:10

2 Answers2

2

Since you're writing Windows 10 universal app, XmlDocument.Save(string) is unavailable. Instead, use

    using (FileStream fs = new FileStream("test.xml", FileMode.Create))
    {
        doc.Save(fs);
    }
Nino
  • 6,931
  • 2
  • 27
  • 42
  • Is this solution working? For me it is giving unauthorized access error – Kiran Paul May 06 '16 at 18:51
  • hi i just wanted to mention that I was working with core net 1.0 in ubuntu 16.04 and I came out the same error.but when i do it this way keeps me xml thank you very much – JPCS Nov 14 '16 at 02:11
1

Since you are working on windows universal app.

you need to use the code below to save file in storage:

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await doc.SaveToFileAsync(file);
Saadi
  • 2,211
  • 4
  • 21
  • 50
  • Error CS1503 Argument 1: cannot convert from 'string' to 'System.IO.Stream' Has this error. – –  May 06 '16 at 06:57
  • Please, see this solution. In UWP, this way help you to save xml file. – Saadi May 06 '16 at 09:25