简体   繁体   中英

Paste text from EditText to xml string

I have EditText field and WebClient.

In EditText user write city.

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

        };

I need to place text from EditText to xml string.

Code of POST request with xml

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

How I can realize this?

Normally you would have a data contract (a set of classes which represent the XML). You then populate this data contract with values. When you are done and want to send it as XML to the server, you take that object and serialize it into XML or JSON or whatever format the service requires. Taking the XML you have shown in the Order and throwing it through a XML 2 C# generator you get this:

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; }
    }
}

You probably need to fix some of the types on some of the properties as the generator is not super clever about this.

Anyways, populate those properties with your values and when you want to create XML from it:

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

Where order is an instance of Order . myXml will be your XML string you can pass on to the server.

EDIT

Since you already have that XML string, you will need to deserialize it into the object, so you can manipulate it:

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

Then you can manipulate order and when you are done, convert it back to XML with:

var myXml = SerializeObject<Order>(order);

You have to make sure your XML is well formed when you use these methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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