XML : Have both DataType and additional Attribute during Xml Serialization

I am attempting to create an XML document through the application of attributes on fields/properties ([XmlAttribute], [XmlElement], etc.) My problem is that I have a requirement that I attach an additional attribute to a primitive datatype in the style of:

  <document>    <binary addAttribute="X" xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64">      [... binary ...]    </binary>  </document>    

I'm making use of code like the following:

  [Serializable]  public class Document {      [XmlElement]      public BinaryObject Binary { get; set; }  }    [Serializable]  public class BinaryObject {      [XmlText(DataType = "base64Binary")]      public byte[] Binary { get; set; }        [XmlAttribute]      public int AddAttribute { get; set; }  }    public class XmlExample {      public static void Main(string[] args)      {          Document document = new Document();          document.Binary = new BinaryObject();          document.Binary.Binary = File.ReadAllBytes(@"FileName");          document.Binary.AddAttribute = 0;            XmlSerializer serializer = new XmlSerializer(typeof(Document));            serializer.Serialize(Console.Out, document);          Console.ReadLine();      }  }    

This, however, provides the following output:

  <document>    <binary addAttribute="X">      [... binary ...]    </binary>  </document>    

If I attempt to move the byte[] Binary to the Document class instead I can get the xmlns:dt="..." as expected but I cannot attach the arbitrary addAttribute when I do so (unless I missed something obvious.)

The question is: Can I do this (have both the DataType and the addAttribute) exclusively through C# attributes?

No comments:

Post a Comment