我想要(反)序列化内部类。
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; }
}
获取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>
我无法编写真正的代码和数据(因为NDA)
我尝试了XmlSerializer,但它需要公共类。
我尝试了DataContractSerializer,并使用内部,但它不支持属性。
我认为,正如您所说,XmlSerializer 可以工作,但它不能工作,因为该类不是公共的。
我建议为此目的编写公共虚拟类:
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; }
}
您应该对它们进行序列化和反序列化,然后在您的真实类之间进行显式类型转换
例如:
Data initialData = ...;
DataDummy initialDataDummyfied = (DataDummy)initialData;
或者反过来:
DataDummy deserializedData = ...;
Data usableData = (Data)initialData;
只要课程相同,这应该可行。