没有集合类的控件数组序列化

时间:2014-09-02 22:06:27

标签: c# serialization

我有这段代码:

public static void Main()
        {
            var test = new TestXML[] {new TestXML() {TestAttribute = "blah"}};

            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;

            var ns = new XmlSerializerNamespaces();
            ns.Add("abc", "a/b/c/d/e/f/g");


            var serializer = new XmlSerializer(typeof(TestXML[]));

            var sw = new StringWriter();
            var xw = XmlWriter.Create(Console.Out, settings);

            serializer.Serialize(xw,test,ns);

            Console.ReadLine();
        }

(续)

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "a/b/c/d/e/f/g",IncludeInSchema = true)]
    public class TestXML
    {
        public string TestAttribute { get; set; }   
    }

代码输出:

 <ArrayOfTestXML
 xmlns:abc="a/b/c/d/e/f/g"><TestXML><abc:TestAttribute>blah</abc:TestAttribute></TestXML></ArrayOfTestXML>

我们有第三方依赖项限制了我们将序列化过程中的集合类添加的能力。是否可以控制序列化以使<TestXML>获得前缀abc?

1 个答案:

答案 0 :(得分:0)

如何不使用标准的XmlSerializer(这对于XML本身没有太多约束并仅用于序列化/反序列化的情况很好)并为您的类编写自己的序列化支持?