简体   繁体   中英

XML Serialization Customized Partially

in brief what I need is that to custom the class project partially

public class project : IXmlSerializable
{
    [XmlAttribute]
    public string name;
    [XmlIgnore]
    public string[] contents;
    public XmlSchema GetSchema()
    {
        return null;
    }

  public void ReadXml(XmlReader reader)
  {
      if (reader.HasAttributes)
      {
          contents=new string[reader.AttributeCount];
          for (int i = 0; i < reader.AttributeCount; i++)
          {
              reader.MoveToAttribute(i);
              string attr = reader.Name;
              Regex reg = new Regex(@"content_\d+");
              if (reg.IsMatch(attr))
              {
                  contents[i] = reader.Value;
              }
          }
      }
  }

  public void WriteXml(XmlWriter writer)
  {
      for (int i = 0; i < contents.Count(); i++)
      {
          writer.WriteAttributeString("content_" + i, contents[i]);
      }
  }
}

I want name to be serialized normally but custom only the contents of the array (I need to make it as attribute in project node ). what happen actually is when I implement the project class the name is kept null any idea how can I do this?

In ReadXml method you never assign name property. Just add

name= "someValue";

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