0

I have a weird problem. i have this 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

this is my model, and i have tried to handle this value in case it comes as 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.

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

6
  • There is no Facilities class in the posted code Commented Mar 9, 2016 at 17:47
  • it fails on on tag when its empty but not on 453 - I don't understand this, can you clarify where and when the deserialization fails. Commented Mar 9, 2016 at 17:48
  • Is that a table [Table("FacilityNew")]? Commented Mar 9, 2016 at 17:50
  • 1
    Related. Commented Mar 9, 2016 at 18:00
  • @CodingGorilla it fails on the second Facility there the MunicipalCode is Empty, but doesnt fail in the first Facility there the Municipal is not empty Commented Mar 9, 2016 at 18:03

1 Answer 1

1

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 "";}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.