Serialization of internal class to xml
Hackish but you could make your class public and mark it with EditorBrowsable attribute to hide it from intellisense.
[EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[Serializable]
public class InternalSerializable
In C#, EditorBrowsable work only on compiled assemblies : Hiding GetHashCode/Equals/ToString from fluent interface classes intellisense in Visual Studio for C#?
Serialize List of classes declared with internal modifier?
You're on the right track... except that the actual serialization is not performed by System.Xml, but by a dynamically generated assembly. You can't predict the name of that assembly (it's randomly generated), so you can't use it in the InternalsVisibleTo
attribute.
The only solution is to pre-generate the XML serialization assembly. You can do that using the XML Serializer Generator Tool (Sgen.exe). The name of the generated assembly will be "YourAssembly.XmlSerializers" ; that's the name you have to use in the InternalsVisibleTo
attribute.
XML deserialisation fails with nested classes (inner classes)
The problem is solved and it had nothing to do with deserialisation of nested classes:
In one of my classes (AgainAnotherClass
), I had following source code:
[XmlElement(ElementName = "SA")]
[XmlElement(ElementName = "SA")]
public string SomeAttribute { get; set; }
(a typical case of Copy/Paste)
The fact that I had two lines with XmlElement
caused the problem.
The exception looked as follows:
InnerException: {"There was an error reflecting field 'f_Inside'."}
Message: "There was an error reflecting type '<NameSpace>.AgainOtherClass'."
The InnerException
made me believe that there was a problem with the nested class, while the Message
spoke about a completely other class. I decided to follow the InnerException
.
That was wrong! So, in case of C# exceptions where InnerException
and Message
contradict each other, first check the Message
, then (maybe) the InnerException
.
Serialize Nested Classes in c#?
What you really want serialize is this :
Person p = new Person();
p.Name = _name;
p.Age = _age;
Adress a = new Adress();
But these variables are local.
Create a property of each one and decorate them with the serializable attribute too. Now it will work.
public SampleClass(string _name, byte _age, string _street, int _number)
{
this.Person = new Person();
Person p = this.Person;
p.Name = _name;
p.Age = _age;
this.Adress = new Adress();
Adress a = this.Adress;
a.Street = _street;
a.Number = _number;
}
[Serializable]
public Person Person { get; set; }
[Serializable]
public Adress Adress { get; set; }
BTW: Address takes 2 d.
How to serialize object in XML while flattening the internal hierarchy of properties within
This should do it
void Main()
{
var foo = new Foo
{
Name = "name",
fooType = "type",
BarObject = new Bar { BarString = "bar"}
};
var s = new XmlSerializer(typeof(Foo));
using (var writer = new StringWriter())
{
s.Serialize(writer, foo);
writer.ToString().Dump();
}
}
public class Foo {
[XmlAttribute("Type")]
public string fooType;
[XmlElement("Name")]
public string Name;
[XmlElement("Message")]
public Bar BarObject;
}
public class Bar {
[XmlText]
public string BarString;
}
The output is:
<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Type="type">
<Name>name</Name>
<Message>bar</Message>
</Foo>
Related Topics
How to Programmatically List All Projects in a Solution
Check If an Executable Exists in the Windows Path
Merge 2 Datatables and Store in a New One
Is Casting the Same Thing as Converting
Why Does Ienumerator<T> Inherit from Idisposable While the Non-Generic Ienumerator Does Not
Simple Way to Display Row Numbers on Wpf Datagrid
Disable Transitive Project Reference in .Net Standard 2
Reversible Shuffle Algorithm Using a Key
Httpcontext.Current.Session Is Null in Ashx File
How to Drag and Drop a Button from One Panel to Another Panel
Disabling Screen Saver and Power Options in C#
Identifying Nhibernate Proxy Classes
A Reproducible Example of Volatile Usage
How to Determine If an Event Is Already Subscribed
How to Export a Gridview.Datasource to a Datatable or Dataset