我有4個物件在4個檔案
第1個檔案
[XmlRoot("DaraSheet")]
public class DataSheet : BaseData
{
[XmlArray("Sheet")]
ArrayList SheetArray;
}
第2個檔案
[XmlRoot("Sheet")]
public class Sheet
{
[XmlElement("Tellphone")]
String Tellphone;
[XmlArray("Personal")]
ArrayList PersonalArray;
}
第3個檔案
[XmlRoot("Personal")]
class Personal
{
[XmlElement("date")]
DateTime date;
[XmlElement("Name")]
String Name;
[XmlElement("Profession")]
Int32 Profession;
[XmlArray("Room")]
ArrayList RoomArray;
}
第4個檔案
[XmlRoot("Room")]
class Room
{
[XmlElement("Roomnumber")]
Int32 Roomnumber;
[XmlElement("Rent")]
Int32 Rent;
}
將物件轉換成XML檔案
public override void WriteXMLData(string Path)
{
XmlSerializer XS = new XmlSerializer(typeof(ArrayList),new Type[]{typeof(DataSheet)});
TextWriter writer = new StreamWriter(Path);
XS.Serialize(writer, SheetArray);
}
補充預計輸出XML格式
<DataSheet>
<Sheet>
<Tellphone></Tellphone>
<Personals>
<Personal>
<date></date>
<Name></Name>
<Profession></Profession>
<Rooms>
<Room>
<Roomnumber></Roomnumber>
<Rent></Rent>
</Room>
<Room>
....
</Room>
</Rooms>
</Personal>
<Personal>
...
</Personal>
</Personals>
</Sheet>
<Sheet>
...
</Sheet>
</DataSheet>
轉換過程中會出現
System.InvalidOperationException: 'There was an error generating the XML document.'
InvalidOperationException: The type DataLibary.Sheet was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
經過1天的努力改成
第1個檔案
[XmlRoot("DaraSheet")]
public class DataSheet : BaseData
{
[XmlElement(Type = typeof(Sheet))]
public ArrayList SheetArray;
}
第2個檔案
[XmlRoot("Sheet")]
public class Sheet
{
[XmlElement("Tellphone")]
String Tellphone;
[XmlElement(Type = typeof(Personal))]
ArrayList PersonalArray;
}
第3個檔案
[XmlRoot("Personal")]
class Personal
{
[XmlElement("date")]
DateTime date;
[XmlElement("Name")]
String Name;
[XmlElement("Profession")]
Int32 Profession;
[XmlElement(Type = typeof(Room))]
ArrayList RoomArray;
}
第4個檔案
[XmlRoot("Room")]
class Room
{
[XmlElement("Roomnumber")]
Int32 Roomnumber;
[XmlElement("Rent")]
Int32 Rent;
}
成功輸出XML
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<anyType xsi:type="Sheet">
<Tellphone>0</Tellphone>
<PersonalArray>
<date>2022-05-01T00:00:00+08:00</date>
<Name />
<Profession>0</Profession>
<RoomArray>
<Roomnumber>0</Roomnumber>
<Rent>0</Rent>
</RoomArray>
<Evaluation />
</PersonalArray>
</anyType>
</ArrayOfAnyType>
現在的問題如下
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<anyType xsi:type="Sheet">
為甚麼不是
<DataSheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Sheet>
已經成更修正
將原先的
public override void WriteXMLData(string Path)
{
XmlSerializer XS = new XmlSerializer(typeof(ArrayList),new Type[]{typeof(DataSheet)});
TextWriter writer = new StreamWriter(Path);
XS.Serialize(writer, SheetArray);
}
改成
public override void WriteXMLData(string Path)
{
XmlSerializer XS = new XmlSerializer(typeof(DataSheet));
TextWriter writer = new StreamWriter(Path);
XS.Serialize(writer, this);
//this為要寫入的物件,這裡指的是DataSheet建立的物件
}
成功輸出預設的XML
<?xml version="1.0" encoding="utf-8"?>
<DataSheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SheetArray>
<Tellphone>0</Tellphone>
<PersonalArray>
<date>2022-05-01T00:00:00+08:00</date>
<Name />
<Profession>0</Profession>
<RoomArray>
<Roomnumber>0</Roomnumber>
<Rent>0</Rent>
</RoomArray>
<Evaluation />
</PersonalArray>
</SheetArray>
</DataSheet>
出去運動一下,馬上有想法就全部解決了
不能自行標記已解決~~哈哈