Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#:从对象转换为xml时,布尔值未转换_C#_Xml - Fatal编程技术网

c#:从对象转换为xml时,布尔值未转换

c#:从对象转换为xml时,布尔值未转换,c#,xml,C#,Xml,我正在尝试将对象转换为xml。我编写了一个将对象转换为xml的类。在我的对象中,我有布尔变量和字符串变量。但是,当我只转换字符串变量时,会转换为布尔变量,而不是布尔变量。请帮我解决这个问题。下面是我的代码 LocateRequest stockLoanRequest = new LocateRequest(); stockLoanRequest.acceptManualApproval = false; stockLoanRequest.acceptPartial = false; stockL

我正在尝试将对象转换为xml。我编写了一个将对象转换为xml的类。在我的对象中,我有布尔变量和字符串变量。但是,当我只转换字符串变量时,会转换为布尔变量,而不是布尔变量。请帮我解决这个问题。下面是我的代码

LocateRequest stockLoanRequest = new LocateRequest();
stockLoanRequest.acceptManualApproval = false;
stockLoanRequest.acceptPartial = false;
stockLoanRequest.market = "US";
stockLoanRequest.cashOrSwap = cashOrSwapType.CASH;
stockLoanRequest.requestedQuantity = request.shares.ToString();
stockLoanRequest.requestorReference = request.locateId;
stockLoanRequest.securityId = request.symbol;
stockLoanRequest.securityIdentifierType = securityIdentifierType.TICKER;
return stockLoanRequest;
            
将对象转换为xml代码:

public static string convertRequestToXML(Object obj)
{
    XmlSerializer serializer = null;
     StringWriterUTF8 sw = new StringWriterUTF8();

     XmlTextWriter tw = null;
     try
     {
         serializer = new XmlSerializer(obj.GetType());
         tw = new XmlTextWriter(sw);
         tw.Formatting = Formatting.Indented;
         serializer.Serialize(tw, obj);
     }
     catch (Exception ex)
     {
         //Handle Exception Code
     }
     finally
     {
         sw.Close();
         if (tw != null)
         {
             tw.Close();
         }
     }

     return sw.ToString();
 }
输出为xml如下所示:

<LocateRequests>
    <LocateRequest securityId="IBM" securityIdentifierType="TICKER" market="US" requestedQuantity="1" requestorReference="34" />
</LocateRequests>

我在上面的输出中没有看到acceptManualApproval和acceptPartial的布尔值

下面是定位请求类:

公共部分类LocateRequest
{        
私人bool-acceptManualApprovalField;
指定的专用布尔接受手册批准字段;
私人布尔-阿尔菲尔德;
专用布尔接受指定的部分字段;
私有字符串安全字段;
private securityIdentifierType securityIdentifierType字段;
私人领域;
私有字符串requestedQuantityField;
私有字符串请求者引用字段;
私人收银员waptype cashOrSwapField;
指定的私人bool cashorswapfield;
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公众认可{
得到{
返回此.acceptManualApprovalField;
}
设置{
此.acceptManualApprovalField=值;
}
}
/// 
[System.Xml.Serialization.XmlIgnoreAttribute()]
指定的公共bool acceptmanualapprovals{
得到{
返回指定的此.AcceptManualApprovalField;
}
设置{
此.acceptManualApprovalFieldSpecified=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共布尔接受部分{
得到{
返回此.acceptPartialField;
}
设置{
this.acceptPartialField=值;
}
}
/// 
[System.Xml.Serialization.XmlIgnoreAttribute()]
公共bool acceptPartialSpecified{
得到{
返回此.acceptPartialFieldSpecified;
}
设置{
this.acceptPartialFieldSpecified=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute(DataType=“token”)]
公共字符串安全ID{
得到{
返回此.securityIdField;
}
设置{
this.securityIdField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共安全标识类型安全标识类型{
得到{
返回此.securityIdentifierTypeField;
}
设置{
this.securityIdentifierTypeField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute(DataType=“token”)]
公共弦市场{
得到{
返回此.marketField;
}
设置{
this.marketField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute(DataType=“positiveInteger”)]
公共字符串requestedQuantity{
得到{
返回此.requestedQuantityField;
}
设置{
this.requestedQuantityField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute(DataType=“token”)]
公共字符串请求引用{
得到{
返回此.requestorReferenceField;
}
设置{
this.requestorReferenceField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共收银员waptype cashOrSwap{
得到{
返回此文件。cashOrSwapField;
}
设置{
此.cashOrSwapField=值;
}
}
/// 
[System.Xml.Serialization.XmlIgnoreAttribute()]
公共银行现金指定{
得到{
返回指定的.cashorswapfield;
}
设置{
this.cashorswapfields specified=值;
}
}
}

后缀为“指定的”的布尔属性有问题,它是忽略没有后缀的属性。例如,acceptPartialSpecified在序列化期间忽略xml中的acceptPartial

要解决此问题,请重命名所有指定的属性,如specifiedPropertyName,或将其从类中删除

名称以“Specified”后缀结尾的成员对XmlSerializer具有特殊意义:它们控制可选ValueType成员的序列化,并且必须是System.Boolean类型


我希望这对您有所帮助。

您能发布
LocateRequest
的代码吗?XmlSerializer只会序列化公共属性,这只是为了确保布尔值是公共属性,。。我已经输入密码了。谢谢你,萨吉德。你太棒了。。它起作用了。。我真的很感激……不客气,我很乐意帮忙。你能接受的答案可能是会帮助别人,检查如何接受答案