使用相同的元素名称序列化XML

时间:2019-01-30 22:14:43

标签: c# xml serialization xml-serialization

我在序列化以下XML时遇到了一些麻烦...

<Activity mc:Ignorable="sap sap2010 sads" x:Class="Main"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System.Activities</x:String>
      <x:String>System.Activities.Statements</x:String>
      <x:String>System.Activities.Expressions</x:String>
    </sco:Collection>
</TextExpression.NamespacesForImplementation>
<TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>Microsoft.VisualBasic</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
    </sco:Collection>
</TextExpression.ReferencesForImplementation>
</Activity>

我创建了以下类来序列化第一个<TextExpression.NamespacesForImplementation>元素,并创建了类似的类来序列化<TextExpression.ReferencesForImplementation>,它们可以单独工作...

[XmlRoot(Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
    public class Activity
    {
        [XmlAttribute(AttributeName = "Ignorable", Namespace = "http://schemas.openxmlformats.org/markup-compatibility/2006")]
        public string Ignorable { get; set; }

        [XmlAttribute(AttributeName = "Class", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
        public string Class { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces Xmlns { get; set; }

        [XmlElement(ElementName = "TextExpression.NamespacesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
        public NamespacesForImplementation NamespacesForImplementation { get; set; }

        [XmlElement(ElementName = "TextExpression.ReferencesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
        public ReferencesForImplementation ReferencesForImplementation { get; set; }
    }

public class NamespacesForImplementation
{
    [XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
    public StringCollection Collection { get; set; }
}

public class ReferencesForImplementation
{
    [XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
    public ReferencesCollection Collection { get; set; }
}

public class StringCollection
{
    [XmlAttribute(AttributeName = "TypeArguments", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
    public string TypeArguments { get; set; }

    [XmlElement(ElementName = "String", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
    public List<string> String { get; set; }
}

public class ReferencesCollection
{
    [XmlAttribute(AttributeName = "TypeArguments", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
    public string TypeArguments { get; set; }

    [XmlElement(ElementName = "AssemblyReference", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
    public List<string> AssemblyReference { get; set; }
}

上述XML在适当的命名空间下有效。尝试序列化两个Collection元素时会出现问题,因为它们都具有不同的内部元素,但是具有相同的元素名称。有什么建议么?我还应该提到我曾尝试在Visual Studio 2017中使用特殊的粘贴选项'XML to C#',但是捕获的结果一旦序列化并在之后立即反序列化,就不会提供输入结果。

1 个答案:

答案 0 :(得分:1)

序列化时,不需要类中的每个属性都有值。参见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Activity activity = new Activity() {
                Ignorable = "sap sap2010 sads",
                Class = "Main",
                NamespacesForImplementation = new NamespacesForImplementation() {
                    Collection = new StringCollection() {
                        TypeArguments = "x:String",
                        String = new List<string>() {
                            "System.Activities", "System.Activities.Statements", "System.Activities.Expressions"
                        }
                    }
                },
                ReferencesForImplementation = new ReferencesForImplementation() {
                    Collection = new StringCollection() {
                        TypeArguments = "AssemblyReference",
                        AssemblyReference = new List<string>() {
                            "System.Activities", "Microsoft.VisualBasic", "mscorlib"
                        }
                    }
                }

            };

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(Activity));
            serializer.Serialize(writer, activity);
        }
    }
    [XmlRoot(Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
    public class Activity
    {
        [XmlAttribute(AttributeName = "Ignorable", Namespace = "http://schemas.openxmlformats.org/markup-compatibility/2006")]
        public string Ignorable { get; set; }

        [XmlAttribute(AttributeName = "Class", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
        public string Class { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces Xmlns { get; set; }

        [XmlElement(ElementName = "TextExpression.NamespacesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
        public NamespacesForImplementation NamespacesForImplementation { get; set; }

        [XmlElement(ElementName = "TextExpression.ReferencesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
        public ReferencesForImplementation ReferencesForImplementation { get; set; }
    }

    public class NamespacesForImplementation
    {
        [XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
        public StringCollection Collection { get; set; }
    }
    public class ReferencesForImplementation
    {
        [XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
        public StringCollection Collection { get; set; }
    }

    public class StringCollection
    {
        [XmlAttribute(AttributeName = "TypeArguments", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
        public string TypeArguments { get; set; }

        [XmlElement(ElementName = "String", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
        public List<string> String { get; set; }

        [XmlElement(ElementName = "AssemblyReference", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
        public List<string> AssemblyReference { get; set; }
    }
}
相关问题