问题描述
这来自WSDL文档:
<s:complexType name="ResponseMsg">
<s:all>
<s:element minOccurs="0" maxOccurs="1" name="Responsestatus" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="CurBalance" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="AvlBalance" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="AckNowledgement" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Loadamount" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="Bill_Amt_Approved" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="Update_Balance" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="New_Balance_Sequence_Exthost" type="s:long" />
<s:element minOccurs="0" maxOccurs="1" name="CVV2_Result" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="AvlBalance_GPS_STIP" type="s:double" /> <s:element minOccurs="0" maxOccurs="1" name="CurBalance_GPS_STIP" type="s:double" />
</s:all>
</s:complexType>
将WSDL添加到我的Visual Studio项目(添加服务参考)之后,我得到了:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml","4.8.3752.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class ResponseMsg : object,System.ComponentModel.INotifyPropertyChanged {
private string responsestatusField;
private double curBalanceField;
private bool curBalanceFieldSpecified;
private double avlBalanceField;
private bool avlBalanceFieldSpecified;
private string ackNowledgementField;
private double loadamountField;
private bool loadamountFieldSpecified;
private double bill_Amt_ApprovedField;
private bool bill_Amt_ApprovedFieldSpecified;
private int update_BalanceField;
private bool update_BalanceFieldSpecified;
private long new_Balance_Sequence_ExthostField;
private bool new_Balance_Sequence_ExthostFieldSpecified;
private string cVV2_ResultField;
private double avlBalance_GPS_STIPField;
private bool avlBalance_GPS_STIPFieldSpecified;
private double curBalance_GPS_STIPField;
private bool curBalance_GPS_STIPFieldSpecified;
/// <remarks/>
public string Responsestatus {
get {
return this.responsestatusField;
}
set {
this.responsestatusField = value;
this.RaisePropertyChanged("Responsestatus");
}
}
/// <remarks/>
public double CurBalance {
get {
return this.curBalanceField;
}
set {
this.curBalanceField = value;
this.RaisePropertyChanged("CurBalance");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool CurBalanceSpecified {
get {
return this.curBalanceFieldSpecified;
}
set {
this.curBalanceFieldSpecified = value;
this.RaisePropertyChanged("CurBalanceSpecified");
}
}
/// <remarks/>
public double AvlBalance {
get {
return this.avlBalanceField;
}
set {
this.avlBalanceField = value;
this.RaisePropertyChanged("AvlBalance");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AvlBalanceSpecified {
get {
return this.avlBalanceFieldSpecified;
}
set {
this.avlBalanceFieldSpecified = value;
this.RaisePropertyChanged("AvlBalanceSpecified");
}
}
/// <remarks/>
public string AckNowledgement {
get {
return this.ackNowledgementField;
}
set {
this.ackNowledgementField = value;
this.RaisePropertyChanged("AckNowledgement");
}
}
/// <remarks/>
public double Loadamount {
get {
return this.loadamountField;
}
set {
this.loadamountField = value;
this.RaisePropertyChanged("Loadamount");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool LoadamountSpecified {
get {
return this.loadamountFieldSpecified;
}
set {
this.loadamountFieldSpecified = value;
this.RaisePropertyChanged("LoadamountSpecified");
}
}
/// <remarks/>
public double Bill_Amt_Approved {
get {
return this.bill_Amt_ApprovedField;
}
set {
this.bill_Amt_ApprovedField = value;
this.RaisePropertyChanged("Bill_Amt_Approved");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool Bill_Amt_ApprovedSpecified {
get {
return this.bill_Amt_ApprovedFieldSpecified;
}
set {
this.bill_Amt_ApprovedFieldSpecified = value;
this.RaisePropertyChanged("Bill_Amt_ApprovedSpecified");
}
}
/// <remarks/>
public int Update_Balance {
get {
return this.update_BalanceField;
}
set {
this.update_BalanceField = value;
this.RaisePropertyChanged("Update_Balance");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool Update_BalanceSpecified {
get {
return this.update_BalanceFieldSpecified;
}
set {
this.update_BalanceFieldSpecified = value;
this.RaisePropertyChanged("Update_BalanceSpecified");
}
}
/// <remarks/>
public long New_Balance_Sequence_Exthost {
get {
return this.new_Balance_Sequence_ExthostField;
}
set {
this.new_Balance_Sequence_ExthostField = value;
this.RaisePropertyChanged("New_Balance_Sequence_Exthost");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool New_Balance_Sequence_ExthostSpecified {
get {
return this.new_Balance_Sequence_ExthostFieldSpecified;
}
set {
this.new_Balance_Sequence_ExthostFieldSpecified = value;
this.RaisePropertyChanged("New_Balance_Sequence_ExthostSpecified");
}
}
/// <remarks/>
public string CVV2_Result {
get {
return this.cVV2_ResultField;
}
set {
this.cVV2_ResultField = value;
this.RaisePropertyChanged("CVV2_Result");
}
}
/// <remarks/>
public double AvlBalance_GPS_STIP {
get {
return this.avlBalance_GPS_STIPField;
}
set {
this.avlBalance_GPS_STIPField = value;
this.RaisePropertyChanged("AvlBalance_GPS_STIP");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AvlBalance_GPS_STIPSpecified {
get {
return this.avlBalance_GPS_STIPFieldSpecified;
}
set {
this.avlBalance_GPS_STIPFieldSpecified = value;
this.RaisePropertyChanged("AvlBalance_GPS_STIPSpecified");
}
}
/// <remarks/>
public double CurBalance_GPS_STIP {
get {
return this.curBalance_GPS_STIPField;
}
set {
this.curBalance_GPS_STIPField = value;
this.RaisePropertyChanged("CurBalance_GPS_STIP");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool CurBalance_GPS_STIPSpecified {
get {
return this.curBalance_GPS_STIPFieldSpecified;
}
set {
this.curBalance_GPS_STIPFieldSpecified = value;
this.RaisePropertyChanged("CurBalance_GPS_STIPSpecified");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this,new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
public static string ObjectToXml<T>(this T obj) where T : class
{
var settings = new XmlWriterSettings
{
Indent = true,OmitXmlDeclaration = false,Encoding = Encoding.GetEncoding("UTF-8")
};
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty,string.Empty);
var serializer = new XmlSerializer(obj.GetType());
using (var stringWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter,settings))
{
serializer.Serialize(xmlWriter,obj,namespaces);
}
return stringWriter.ToString();
}
}
并这样称呼它:
var rtnAck = new ResponseMsg
{
Responsestatus = "00",CurBalance = 0.5,AvlBalance = 0.5,AckNowledgement = "1",Loadamount = 0.00,CVV2_Result = "test"
};
var rtnData = rtnAck.ObjectToXml();
但是结果字符串是
<?xml version="1.0" encoding="utf-16"?>
<ResponseMsg>
<Responsestatus xmlns="http://tempuri.org/">00</Responsestatus>
<AckNowledgement xmlns="http://tempuri.org/">1</AckNowledgement>
<CVV2_Result xmlns="http://tempuri.org/">test</CVV2_Result>
</ResponseMsg>
我要为其设置值的其他字段在哪里?唯一出现的是string类型,缺少的是double。
此外,如何摆脱每个元素上的xmlns?
解决方法
您看到以“ Specified”结尾的属性吗?这些是存在的,因此可以区分“属性存在”和“属性缺失”的情况。对于字符串属性,它由!= null
或== null
隐含。
因此对于非字符串属性,如果要序列化值,则需要将... Specified属性设置为true:
var rtnAck = new ResponseMsg
{
Responsestatus = "00",CurBalance = 0.5,CurBalanceSpecified = true,AvlBalance = 0.5,AvlBalanceSpecified = true,Acknowledgement = "1",LoadAmount = 0.00,LoadAmountSpecified = true,CVV2_Result = "test"
};
这不是非常用户友好,但是XmlSerialization早于Nullable<T>
。