C# 将文本从EditText粘贴到xml字符串
我有EditText字段和WebClient 在EditText中,用户写入城市C# 将文本从EditText粘贴到xml字符串,c#,android,xml,xamarin,xamarin.android,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
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-misto=findviewbyd(Resource.Id.misto);
TextView one=findviewbyd(Resource.Id.parentContainer);
TextView 2=FindViewById(Resource.Id.clicklistener1);
TextView三=FindViewById(Resource.Id.clicklistener2);
TextView-four=findviewbyd(Resource.Id.clicklistener3);
misto.TextChanged+=(对象发送者,Android.Text.textchangedventargs 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);
};
nadislati.Click+=委托
{
使用(var client=new WebClient())
{
var values=新的NameValueCollection();
值[“顺序”]=“\n\n\n\n\n\n\n\n\n”;
值[“OrderText”]=“hello”;
var response=client.UploadValues(“http://193.203.48.54:5000/fastoperator.asmx/AddOrder“、价值观);
var responseString=Encoding.UTF8.GetString(响应);
}
可控震源vib=(可控震源)this.GetSystemService(Context.可控震源服务);
振动(30);
var intent31=新的意图(该,类型为(Cart3Activity));
起始触觉(意图31);
};
如何实现这一点?通常,您会有一个数据契约(一组表示XML的类)。然后用值填充此数据协定。当您完成并希望将其作为XML发送到服务器时,可以获取该对象并将其序列化为XML或JSON或服务所需的任何格式。将您在
顺序中显示的XML放入
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);
然后,您可以操作顺序
,完成后,使用以下命令将其转换回XML:
var myXml = SerializeObject<Order>(order);
var myXml=serialized对象(顺序);
使用这些方法时,您必须确保XML格式正确。我不能使用变量,即从EditText写入文本并将此变量粘贴到我已有的XML字符串中?就像您可以序列化XML一样,您可以反序列化到对象中。我将编辑我的答案。
var myXml = SerializeObject<Order>(order);