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

Serializing XML With Same Element Name

我在序列化以下内容时遇到了一些问题 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#',但是捕获的结果不提供序列化和反序列化之后的输入结果。

序列化时不需要为 class 中的每个 属性 赋值。请参阅下面的代码:

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; }
    }
}