简体   繁体   中英

xml de-serialization related problem occur

suppose i have xml data stored in string variable. so when i am trying to de-serialize xml to my class then i am getting error for property name.

  [XmlAttribute("Name")]
  public string CompanyName
  {
    get
    {
      return __CompanyName;
    }
    set
    {
      if (value != null)
        __CompanyName = value;
      else
        __CompanyName = "";
    }
  }

actually i am getting error because in my xml there is Name tag but in my code the property name is CompanyName. that is why i am getting error. is there any way to map the property in my property as a result Name will be mapped with CompanyName and no problem occur during deserialization. please help. [XmlAttribute("Name")]... it should be xmlelement instead of XmlAttribute. need help.

Changing the name is fine; you just need the right attributes. You don't show the XML, but it looks like you just need

[XmlElement("Name")]
public string CompanyName {...etc...}

If the type is outside your control, you can use XmlAttributeOverrides to re-educate XmlSerializer - but if you do that you must cache and re-use the serializer instance (or you will leak memory).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM