在 Xml 序列化期间同时具有数据类型和附加属性

Have both DataType and additional Attribute during Xml Serialization

我试图通过在 fields/properties([XmlAttribute][XmlElement] 等)上应用属性来创建 XML 文档。我的问题是我有要求我以以下样式将附加属性附加到原始数据类型:

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

我正在使用如下代码:

[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();
    }
}

但是,这会提供以下输出:

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

如果我尝试将 byte[] Binary 移动到 Document class 而不是我可以按预期获得 xmlns:dt="..." 但我无法附加任意 addAttribute 当我这样做时(除非我错过了一些明显的东西。)这是不正确的;我在 XML 中误读了我从 XML 出来的东西。本例中未添加 xmlns:dt 元素。

问题是:我可以完全通过 C# 属性来执行此操作(同时具有 DataType 和 addAttribute)吗?

试试这个

using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    [XmlRoot("Document")]
    public class Document
    {
        [XmlText(DataType = "base64Binary")]
        public byte[] Binary { get; set; }
        [XmlAttribute]
        public int AddAttribute { get; set; }
    }

}
​

这个问题的部分答案来自这里:。 DataType = "base64Binary" 而不是 xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64" 应用于它所附加的元素。必须向 BinaryObject 添加一个属性,为 dt:dt = "bin.base64" 提供正确的名称 space.

最终代码

[Serializable]
public class Document {
    public BinaryObject Binary { get; set; }
}

[Serializable]
public class BinaryObject {
    [XmlText]
    public byte[] Binary { get; set; }

    [XmlAttribute]
    public int AddAttribute { get; set; }

    // Adds the dt:dt object to the correct name space.
    [XmlAttribute("dt", Namespace = "urn:schemas-microsoft-com:datatypes")]
    public string DataType { get; set; }

    public BinaryObject() { DataType = "bin.base64"; }
}


public class XmlExample {
    public static void Main(string[] args)
    {
        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

        // Adds the needed namespace to the document.
        namespaces.Add("dt", "urn:schemas-microsoft-com:datatypes");

        Document document = new Document();
        document.Binary = new BinaryObject();
        document.Binary.Binary = new byte[]{0,1,2,3,4,5,6,7,8,9};
        document.Binary.AddAttribute = 0;

        XmlSerializer serializer = new XmlSerializer(typeof(Document));

        serializer.Serialize(Console.Out, document, namespaces);
        Console.ReadLine();
    }
}

最终输出

<Document xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <Binary AddAttribute="0" dt:dt="bin.base64">AAECAwQFBgcICQ==</Binary>
</Document>