【问题标题】:Paste text from EditText to xml string将文本从 EditText 粘贴到 xml 字符串
【发布时间】:2015-11-29 13:27:01
【问题描述】:

我有 EditText 字段和 WebClient。

在 EditText 用户写城市。

EditText misto = FindViewById<EditText>(Resource.Id.misto) ;
        TextView one = FindViewById<TextView>(Resource.Id.parentContainer);
        TextView two = FindViewById<TextView>(Resource.Id.clicklistener1);
        TextView three = FindViewById<TextView>(Resource.Id.clicklistener2);
        TextView four = FindViewById<TextView>(Resource.Id.clicklistener3);
        misto.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {

            var city = e.Text.ToString ();

        };

我需要将 EditText 中的文本放置到 xml 字符串中。

带有xml的POST请求代码

nadislati.Click += delegate
        {

            using (var client = new WebClient())
            {

                var values  = new NameValueCollection();
                values["Order"] = "<Order \n CallConfirm=\"1\"\n PayMethod=\"Безнал\" \n QtyPerson=\"2\" \n Type=\"2\" \n PayStateID=\"0\" \n Remark=\"тестовый заказ с мобильного приложения. просьба при получении заказа переслать скриншот на имейл ....@\" \n RemarkMoney=\"0\" \n TimePlan=\"\" \n Brand=\"1\" \n DiscountPercent=\"0\" \n BonusAmount=\"0\"\n Department=\"\"\n >\n <Customer Login=\"suhomlineugene@gmail.com\" FIO=\"Evgenyi Sukhomlin\"/>\n <Address \n CityName=\"\" \n StationName=\"\" \n StreetName=\"\" \n House=\"\" \n Corpus=\"\" \n Building=\"\" \n Flat=\"\" \n Porch=\"\" \n Floor=\"\" \n DoorCode=\"\"\n />\n\n <Phone Code=\" 096\" Number=\"50 526-43-19\" />\n <Products>\n <Product Code=\"574\" Qty=\"1\" />\n </Products>\n </Order>";
                values["OrderText"] = "hello";
                var response  = client.UploadValues("http://193.203.48.54:5000/fastoperator.asmx/AddOrder", values);

                var responseString = Encoding.UTF8.GetString(response); 

            }

            Vibrator vib = (Vibrator)this.GetSystemService(Context.VibratorService);
            vib.Vibrate(30);
            var intent31 = new Intent(this, typeof(Cart3Activity));


            StartActivity(intent31);
        };

我怎样才能意识到这一点?

【问题讨论】:

    标签: c# android xml xamarin xamarin.android


    【解决方案1】:

    通常你会有一个数据契约(一组代表 XML 的类)。然后,您使用值填充此数据协定。当您完成并希望将其作为 XML 发送到服务器时,您可以将该对象序列化为 XML 或 JSON 或服务所需的任何格式。将您在 Order 中显示的 XML 放入 XML 2 C# generator 中,您将得到:

    using System;
    using System.Xml.Serialization;
    using System.Collections.Generic;
    
    namespace Xml2CSharp
    {
        [XmlRoot(ElementName="Customer")]
        public class Customer {
            [XmlAttribute(AttributeName="Login")]
            public string Login { get; set; }
            [XmlAttribute(AttributeName="FIO")]
            public string FIO { get; set; }
        }
    
        [XmlRoot(ElementName="Address")]
        public class Address {
            [XmlAttribute(AttributeName="CityName")]
            public string CityName { get; set; }
            [XmlAttribute(AttributeName="StationName")]
            public string StationName { get; set; }
            [XmlAttribute(AttributeName="StreetName")]
            public string StreetName { get; set; }
            [XmlAttribute(AttributeName="House")]
            public string House { get; set; }
            [XmlAttribute(AttributeName="Corpus")]
            public string Corpus { get; set; }
            [XmlAttribute(AttributeName="Building")]
            public string Building { get; set; }
            [XmlAttribute(AttributeName="Flat")]
            public string Flat { get; set; }
            [XmlAttribute(AttributeName="Porch")]
            public string Porch { get; set; }
            [XmlAttribute(AttributeName="Floor")]
            public string Floor { get; set; }
            [XmlAttribute(AttributeName="DoorCode")]
            public string DoorCode { get; set; }
        }
    
        [XmlRoot(ElementName="Phone")]
        public class Phone {
            [XmlAttribute(AttributeName="Code")]
            public string Code { get; set; }
            [XmlAttribute(AttributeName="Number")]
            public string Number { get; set; }
        }
    
        [XmlRoot(ElementName="Product")]
        public class Product {
            [XmlAttribute(AttributeName="Code")]
            public string Code { get; set; }
            [XmlAttribute(AttributeName="Qty")]
            public string Qty { get; set; }
        }
    
        [XmlRoot(ElementName="Products")]
        public class Products {
            [XmlElement(ElementName="Product")]
            public Product Product { get; set; }
        }
    
        [XmlRoot(ElementName="Order")]
        public class Order {
            [XmlElement(ElementName="Customer")]
            public Customer Customer { get; set; }
            [XmlElement(ElementName="Address")]
            public Address Address { get; set; }
            [XmlElement(ElementName="Phone")]
            public Phone Phone { get; set; }
            [XmlElement(ElementName="Products")]
            public Products Products { get; set; }
            [XmlAttribute(AttributeName="CallConfirm")]
            public string CallConfirm { get; set; }
            [XmlAttribute(AttributeName="PayMethod")]
            public string PayMethod { get; set; }
            [XmlAttribute(AttributeName="QtyPerson")]
            public string QtyPerson { get; set; }
            [XmlAttribute(AttributeName="Type")]
            public string Type { get; set; }
            [XmlAttribute(AttributeName="PayStateID")]
            public string PayStateID { get; set; }
            [XmlAttribute(AttributeName="Remark")]
            public string Remark { get; set; }
            [XmlAttribute(AttributeName="RemarkMoney")]
            public string RemarkMoney { get; set; }
            [XmlAttribute(AttributeName="TimePlan")]
            public string TimePlan { get; set; }
            [XmlAttribute(AttributeName="Brand")]
            public string Brand { get; set; }
            [XmlAttribute(AttributeName="DiscountPercent")]
            public string DiscountPercent { get; set; }
            [XmlAttribute(AttributeName="BonusAmount")]
            public string BonusAmount { get; set; }
            [XmlAttribute(AttributeName="Department")]
            public string Department { get; set; }
        }
    }
    

    您可能需要修复某些属性上的某些类型,因为生成器在这方面不是很聪明。

    无论如何,用你的值填充这些属性,并在你想从中创建 XML 时:

    public static string SerializeObject<T>(this T toSerialize)
    {
        var xmlSerializer = new XmlSerializer(toSerialize.GetType());
    
        using(var textWriter = new StringWriter())
        {
            xmlSerializer.Serialize(textWriter, toSerialize);
            return textWriter.ToString();
        }
    }
    
    var myXml = SerializeObject<Order>(order);
    

    其中orderOrder 的一个实例。 myXml 将是您可以传递给服务器的 XML 字符串。

    编辑

    由于您已经拥有该 XML 字符串,您需要将其反序列化为对象,以便您可以对其进行操作:

    public static T DeserializeObject<T>(string xml)
    {
        using (var ms = new MemoryStream())
        using (var writer = new StreamWriter(ms))
        {
            writer.Write(xml);
            ms.Position = 0;
    
            var serializer = new XmlSerializer(typeof(T));
            using (var reader = XmlReader.Create(ms))
                return (T)serializer.Deserialize(reader);
        }
    }
    
    var order = DeserializeObject<Order>(xmlOrder);
    

    然后你可以操作order,当你完成后,将它转换回XML:

    var myXml = SerializeObject<Order>(order);
    

    当您使用这些方法时,您必须确保您的 XML 格式正确。

    【讨论】:

    • 我不能在从 EditText 写入文本的地方使用变量并将这个变量粘贴到我已经拥有的 xml 字符串中?
    • 就像您可以序列化 XML 一样,您也可以反序列化到对象中。我将编辑我的答案。
    猜你喜欢
    • 2021-05-14
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2018-01-12
    相关资源
    最近更新 更多