【问题标题】:Serializing XML With Same Element Name序列化具有相同元素名称的 XML
【发布时间】:2019-01-30 22:14:43
【问题描述】:

我在序列化以下 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>

我创建了以下类来序列化第一个 &lt;TextExpression.NamespacesForImplementation&gt; 元素,并创建了类似的类来序列化单独工作的 &lt;TextExpression.ReferencesForImplementation&gt;...

[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#”,但捕获的结果在序列化和反序列化后立即不提供输入结果。

【问题讨论】:

  • Activity 的结束标签在哪里?它丢失了,因此 xml 无效。
  • @jdweng 抱歉,这已添加到原始帖子中,但发布后没有出现。我现在已经修改了。
  • 类 ReferencesForImplementation 在哪里?
  • @jdweng 我也会将其添加到原始帖子中!感谢您对帖子的改进。

标签: c# xml serialization xml-serialization


【解决方案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; }
    }
}

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2017-02-07
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多