我希望能够轻松地将大量数据存储到XML文件中。在我使用结构并且运行良好之前,但现在我想使用类,请参阅下面的示例。保存到XML时,子类不会保存到XML。
在XML文件中,我看到了一堆
<logic>
<logicData xsi:nil="true" />
<logicData xsi:nil="true" />
<logicData xsi:nil="true" />
这是我目前使用的代码
public class colorProfileSettings
{
public int[] btn_background = new int[16];
public int[] btn_input = new int[16];
public int[] btn_press = new int[16];
}
public class logicData
{
public colorProfileSettings[] colors = new colorProfileSettings[2];
public int configBtnLedTest = 0;
public int configBtnDimPlus = 0;
public int configBtnDimMin = 0;
public int configBtnCleaning = 0;
public int configBtnDayNightToggle = 0;
public int groupBg = 0;
public int groupLedTest = 0;
public int groupDimming = 10;
}
public class ClassConfigData
{
public logicData[] logic = new logicData[100];
public string fileLocName;
}
public ClassConfigData data = new ClassConfigData();
public void WriteXML(string fileLoc)
{
try
{
if (System.IO.File.Exists(fileLoc))
{
XmlSerializer writer = new XmlSerializer(typeof(ClassConfigData));
StreamWriter file = new StreamWriter(@fileLoc);
writer.Serialize(file, data);
file.Close();
}
else
{
throw new ArgumentNullException("Not a valid file selected.");
}
}
catch (Exception e)
{
throw e;
}
}
答案 0 :(得分:1)
问题是你永远不会实例化任何logicData
数组元素。
我将您的ClassConfigData
课程更改为以下内容,将您的logicData
课程更改为以下内容,并且按预期工作。
public class ClassConfigData
{
public logicData[] logic = new logicData[100];
public string fileLocName;
public ClassConfigData()
{
for (int i = 0; i < logic.Length; i++)
{
logic[i] = new logicData();
}
}
}
public class logicData
{
public colorProfileSettings[] colors = new colorProfileSettings[2];
public int configBtnLedTest = 0;
public int configBtnDimPlus = 0;
public int configBtnDimMin = 0;
public int configBtnCleaning = 0;
public int configBtnDayNightToggle = 0;
public int groupBg = 0;
public int groupLedTest = 0;
public int groupDimming = 10;
public logicData()
{
for (int i = 0; i < colors.Length; i++)
{
colors[i] = new colorProfileSettings();
}
}
}
然后我跑了WriteXML("test.xml");
,它确实应该有它。
<?xml version="1.0" encoding="utf-8"?>
<ClassConfigData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<logic>
<logicData>
<colors>
<colorProfileSettings>
<btn_background>
<int>0</int>
...
</btn_background>
...
</colorProfileSettings>
...
</colors>
<configBtnLedTest>0</configBtnLedTest>
<configBtnDimPlus>0</configBtnDimPlus>
<configBtnDimMin>0</configBtnDimMin>
<configBtnCleaning>0</configBtnCleaning>
<configBtnDayNightToggle>0</configBtnDayNightToggle>
<groupBg>0</groupBg>
<groupLedTest>0</groupLedTest>
<groupDimming>10</groupDimming>
</logicData>
<logicData>
<colors>
<colorProfileSettings>
<btn_background>
<int>0</int>
...
</btn_background>
...
</colorProfileSettings>
...
</colors>
<configBtnLedTest>0</configBtnLedTest>
<configBtnDimPlus>0</configBtnDimPlus>
<configBtnDimMin>0</configBtnDimMin>
<configBtnCleaning>0</configBtnCleaning>
<configBtnDayNightToggle>0</configBtnDayNightToggle>
<groupBg>0</groupBg>
<groupLedTest>0</groupLedTest>
<groupDimming>10</groupDimming>
</logicData>
...
此外,随时您创建了一个引用类型的数组(class
),然后必须实例数组的每个元素。 否则,它们都是null
,因此在序列化时不会有子属性,因为它们尚不存在!您不必使用structs
执行此操作的原因是因为每次创建structure
类型的数组(值类型)时,CLR
都会清除数组的内存为每个元素的struct
类型的新实例。这也是struct
在constructor
中创建structure
时必须为每个属性指定值的原因。
以下代码演示了这一事实:
public struct StructureType
{
public int number;
public int otherNumber;
}
public class ClassType
{
public int number;
public int otherNumber;
}
StructureType[] s1 = new StructureType[2];
s1[0].number = 25;
ClassType[] c1 = new ClassType[2];
c1[0].number = 25;
由于NullReferenceException
不存在,您将在c1[0].number = 25;
行获得c1[0]
。但s1[0].number = 25;
的作用为s1[0]
}默认为内存中的所有零。