iT邦幫忙

0

(1)如何在C#上實作巢狀物件的XML 序列化(以自行解決)(2)為何輸出的XML開始的兩行與預設的不一樣(以自行解決)

  • 分享至 

  • xImage

我有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>

出去運動一下,馬上有想法就全部解決了
不能自行標記已解決~~哈哈

fillano iT邦超人 1 級 ‧ 2022-05-01 09:38:35 檢舉
DataSheet跟Sheet看起來沒關連?
在我碰到JSON之後,就不再用XML來做資料轉換的事了
我用newtonsoft的JSONConvert.Serialize 特別在web跟app之間顯的比XML容易上手
b0505726 iT邦新手 5 級 ‧ 2022-05-01 13:03:32 檢舉
japhenchen 我沒接觸過JSON
單機應用程式也可以使用?
如果要改用JSON要如何改動?
謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答