Reputation: 99
I have been having some trouble serializing the following 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>
I have created the following classes to serialize the first <TextExpression.NamespacesForImplementation>
element and created similar classes to serialize the <TextExpression.ReferencesForImplementation>
which work individually...
[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; }
}
The above XML is valid with appropriate namespaces. The issue arises when attempting to serialize both Collection
elements, since they both have different inner elements but have the same element name. Any suggestions? I should also mention I have tried using the special paste option 'XML to C#' in Visual Studio 2017, but the result captured does not provide the input result once serialized and deserialized immediately after.
Upvotes: 0
Views: 129
Reputation: 34421
You do not need for each property in a class a value when serializing. See code below :
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; }
}
}
Upvotes: 1