1

I want to (de)serialize internal classes.

internal class Data
{
    public string? Name { get; set; }
    public string? Type { get; set; }
    public List<DataWithAttribute> DataList { get; set; }
}

internal class DataWithAttribute
{
    public string? FirstAttribute { get; set; }
    public string? SecondAttribute { get; set; }
}

to get xml

<Data>
    <Name>Some name</Name>
    <Type>Some value</Type>
    <DataList>
        <DataWithAttribute FirstAttribute="some value" SecondAttribute="some value" />
        <DataWithAttribute FirstAttribute="some value" SecondAttribute="some value" />
        <DataWithAttribute FirstAttribute="some value" SecondAttribute="some value" />
    </DataList>
</Data>

I cannot write real code and data (because NDA)

I tried XmlSerializer, but It requires public classes.

So, I tried DataContractSerializer: it works with internal, but it doesn't support attributes.

13
  • what exactly do you mean by "attributes"? As in <Data FirstAttribute="1" SecondAttribute="2"/>? In that case I think you need to rethink your structure, as a list of similar attributes doesn't make much sense. Commented Jun 28, 2024 at 9:58
  • @MakePeaceGreatAgain I want exactly two attributes, not a list. Commented Jun 28, 2024 at 10:13
  • but the property DataWithAttribute literally is a list. Anyway you cannot use complex types for xml-attributes. You have to serialize every attribute seperatly to a simple type. Commented Jun 28, 2024 at 10:31
  • You'd need to write your own serializer for this. I doubt there's any existing that can flatten your list of complex types to attributes in xml. Commented Jun 28, 2024 at 10:40
  • 1
    Being pragmatic: make them public. You could serialize manually, but that's a massive PITA to do efficiently (the reader/writer API), and to do it the convenient way (DOM) has lots of intermediate overhead. Commented Jun 28, 2024 at 11:19

1 Answer 1

0

I assume, as you said XmlSerializer works, but it doesn't because the class is not public.

I would recommend to write public dummy classes for this purpose:

public class DataDummy
{
    public string? Name { get; set; }
    public string? Type { get; set; }
    public List<DataWithAttribute> DataList { get; set; }
}

public class DataWithAttributeDummy
{
    public string? FirstAttribute { get; set; }
    public string? SecondAttribute { get; set; }
}

You should serialize and deserialize to them, and afterwards you do an explicit TypeConversion from and to your real Classes

For Example:

Data initialData = ...;
DataDummy initialDataDummyfied = (DataDummy)initialData;

or the other way around:

DataDummy deserializedData = ...;
Data usableData = (Data)initialData;

as long as the classes are the same, this should work.

Sign up to request clarification or add additional context in comments.

1 Comment

this wouldn't work unless we override the explicit/implicit operator for each of those.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.