将EditText中的文本粘贴到xml字符串

时间:2015-11-29 13:27:02

标签: c# android xml xamarin xamarin.android

我有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);
        };

我怎么能意识到这一点?

1 个答案:

答案 0 :(得分:0)

通常你会有一个数据契约(一组代表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格式正确。