[英]input string was not in a correct format when deserialize xml
I have a weird problem. 我有一个奇怪的问题。 i have this Xml 我有这个Xml
<?xml version="1.0" encoding="utf-8"?>
<Facilities RunTime="2016-03-09 14:18:11">
<Facility ID="789">
<Name>Facility 4</Name>
<Contact />
<AreaName>Center</AreaName>
<MunicipalCode>453</MunicipalCode>
<SMSInfo />
<Materials />
</Facility>
<Facility ID="-1">
<Name>Facility 2</Name>
<Contact />
<AreaName>Mark</AreaName>
<MunicipalCode />
<SMSInfo />
<Materials />
</Facility>
</Facilities>
When i try to deserialize this xml, it fails on on tag <MunicipalCode />
when its empty (see the element MunicipalCode in the second root Facility) but not on <MunicipalCode>453</MunicipalCode>
(in the first root) so when i change the empty one to <MunicipalCode>test test </MunicipalCode>
then it doesnt fail 当我尝试反序列化此xml时,它在标签<MunicipalCode />
为空时失败(请参见第二个根工具中的元素MunicipalCode),但在<MunicipalCode>453</MunicipalCode>
(第一个根目录)中却没有,所以当我将空的更改为<MunicipalCode>test test </MunicipalCode>
然后它不会失败
this is my model, and i have tried to handle this value in case it comes as null. 这是我的模型,并且我尝试过处理此值,以防它为null。
[Table("FacilityNew")]
public class FacilityNew
{
[XmlAttribute("ID"), Key]
public int Id { get; set; }
[XmlElement("SMSInfo")]
public virtual SMSInfo smsInfos { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Contact")]
public string Contact { get; set; }
[XmlElement("AreaName")]
public string AreaNameID { get; set; }
[XmlIgnore]
public string MunicipalCode { get; set; }
[XmlElement("MunicipalCode")]
[Browsable(false)] // not displayed in grids
[EditorBrowsable(EditorBrowsableState.Never)] // not displayed by intellisense
public string MunicipalCodeStirng
{
get
{
if ((MunicipalCode) != null)
{
return MunicipalCode;
}
else
return "";
}
set
{
if ((value)!=null)
{
MunicipalCode = value;
}
else
{
MunicipalCode = "";
}
}
}
[XmlArray("Materials")]
[XmlArrayItem("Material")]
public virtual List<Material> Materials { get; set; }
public FacilityNew()
{
this.Materials = new List<Material>();
}
}
but it still fails its weird because the other empty tags doesn't fail, and i got "input string was not in a correct format " and if i change the name of this tag to <MunicipalCodeASDF />
or something else, it doesn't fail. 但是它仍然失败,因为其他空标签没有失败,并且我得到“输入字符串的格式不正确”,并且如果我将此标签的名称更改为<MunicipalCodeASDF />
或其他名称,它不会不会失败。
this is how i deserialize, 这就是我反序列化的方式
XmlSerializer deserializer = new XmlSerializer(typeof(FacilityNew));
StreamReader sr = new StreamReader(path);
allaFacilities = (FacilityNew)deserializer.Deserialize(sr);
what is the problem 问题是什么
the problem is solved by adding a regex inside the deserializer 通过在反序列化器内部添加一个正则表达式可以解决问题
public static T Deserialize<T>(string xml){
XmlSerializer xs = new XmlSerializer(typeof(T));
string cleanXml = Regex.Replace(xml, @"<[a-zA-Z].[^(><.)]+/>",
new MatchEvaluator(RemoveText));
MemoryStream memoryStream = new MemoryStream((new UTF8Encoding()).GetBytes(cleanXml));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return (T)xs.Deserialize(memoryStream);
}
static string RemoveText(Match m) { return "";}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.