简体   繁体   English

无法写入创建文件并向其中写入xml

[英]Cannot write create file and write xml to it

I writing iOS app. 我在写iOS应用。

I try to create xml and write it to file. 我尝试创建xml并将其写入文件。

I do it with following code. 我用下面的代码来做。

var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var filePath = System.IO.Path.Combine(documentsPath, "myFile.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", "{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")); 


            Console.WriteLine ("TUT");


            //File.WriteAllText(filePath, doc.OuterXml);
            //doc.Save ("myfile.xml");
            doc.Save (filePath);

            Console.WriteLine (doc.OuterXml);
            Console.WriteLine (filePath);

When I start my app it crashes. 当我启动我的应用程序时,它崩溃了。

I think it because I have wrong code for creating xml file. 我认为是因为我使用错误的代码创建xml文件。

How can I correctly write it? 如何正确书写?

It's a bad idea to use raw acces to xml file. 使用原始acces到xml文件是个坏主意。 You should use object model, it has many advantages. 您应该使用对象模型,它具有许多优点。 Solution for you: 为您提供的解决方案:

    public class Customer
    {
            public String Login{get;set;}
            public String FIO{get;set;}
    }

    public class Phone{
            public String Code{get;set;}
            public String Number{get;set;}
    }

    public class Address{
            public String CityName{get;set;}
            public String StationName{get;set;}
            public String StreetName{get;set;}
            public String House{get;set;}
            public String Corpus{get;set;}
            public String Building{get;set;}
            public Int32 Flat{get;set;}
            public String Porch{get;set;}
            public Int32 Floor{get;set;}
            public String DoorCode { get; set; }
    }

    public class Order
    {
            public Int32 CallConfirm {get;set;}
            public String PayMethod{get;set;}
            public String QtyPerson{get;set;}
            public Int32 Type {get;set;}
            public Int32 PayStateID {get;set;}
            public String Remark {get;set;}
            public Int32 RemarkMoney {get;set;}
            public String TimePlan {get;set;}
            public Int32 Brand {get;set;}
            public Int32 DiscountPercent {get;set;}
            public Int32 BonusAmount {get;set;}
            public String Department {get;set;}

            public Customer Customer  {get;set;}
            public Address Address {get;set;}
            public Phone Phone { get; set; }
            public List<String> Products { get; set; }
    } 

       static void Main(string[] args)
        {
            var order = new Order()
            {
                Address = new Address()
                {
                    CityName = "Yeah"
                },
                Phone = new Phone()
                {
                    Code = "251"
                },
                Customer = new Customer()
                {
                    FIO = "Lev Leshenko"
                },
                CallConfirm = 1
                //...
            };

            var s = new XmlSerializer(typeof(Order));
            using(var f = File.Open("test.xml",FileMode.Create)){
                s.Serialize(f, order);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM