![](/img/trans.png)
[英]how to serialize JSON object lists into file with c# not array
[英]How to serialize an object array in C#?
我使用xsd.exe和這個xml文件生成了ac#class:
<?xml version="1.0" encoding="UTF-8"?>
<Mary>
<Frank>
<Joe>
<Susan>
<Stuff>data</Stuff>
</Susan>
<Susan>
<Stuff>data</Stuff>
</Susan>
</Joe>
<Joe>
<Susan>
<Stuff>data</Stuff>
</Susan>
<Susan>
<Stuff>data</Stuff>
</Susan>
</Joe>
</Frank>
</Mary>
我可以使用數據初始化對象:
var susan = new MaryFrankJoeSusan(){Stuff = "my data"};
var frank = new MaryFrank(){Joe = new MaryFrankJoeSusan[1][]};
frank.Joe[0] = new MaryFrankJoeSusan[1]{susan};
var mary = new Mary { Items = new MaryFrank[1] { frank } };
我正在使用以下序列化它到磁盤:
var serializer = new XmlSerializer(typeof(Mary));
using (Stream stream = new FileStream(@"C:\out.xml", FileMode.Create))
{
var settings = new XmlWriterSettings { Indent = true, NewLineOnAttributes = true, OmitXmlDeclaration = true};
using (XmlWriter writer = new XmlTextWriter(stream, Encoding.Unicode))
{
serializer.Serialize(writer, mary);
writer.Close();
}
}
但是,當序列化程序初始化時,我收到以下錯誤:
error CS0030: Cannot convert type 'MaryFrankJoeSusan[]' to 'MaryFrankJoeSusan'
如何將整個Mary對象序列化為磁盤?
這些生成的類有些東西。
問題正在發生,因為MaryFrank.Joe
被聲明為MaryFrankJoeSusan
對象的二維數組,但是它用XmlArrayItemAttribute
修飾,它告訴序列化程序,當它們當然是MaryFrankJoeSusan
時,該2D數組的每個項目都是MaryFrankJoeSusan
類型MaryFrankJoeSusan[]
。
如果在生成的類中更改此行:
[System.Xml.Serialization.XmlArrayItemAttribute("Susan", typeof(MaryFrankJoeSusan),
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
對此:
[System.Xml.Serialization.XmlArrayItemAttribute("Susan", typeof(MaryFrankJoeSusan[]),
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
然后它將序列化而不會出錯。 但是,您無法獲得所需的結果。 而不是這個:
<Mary>
<Frank>
<Joe>
<Susan>
<Stuff>my data</Stuff>
</Susan>
</Joe>
</Frank>
</Mary>
你會得到這個(注意額外的MaryFrankJoeSusan
標簽):
<Mary>
<Frank>
<Joe>
<Susan>
<MaryFrankJoeSusan>
<Stuff>my data</Stuff>
</MaryFrankJoeSusan>
</Susan>
</Joe>
</Frank>
</Mary>
真正的問題似乎是xsd.exe
工具已經錯誤地生成了類結構。 它不是在heirarchy中創建一個代表Joe的類,而是試圖將Joe和Susan組合在一起,這在這里並不真正起作用。
我從問題中通過工具運行原始XML來生成XSD架構,我得到了這個:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Mary" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Mary" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Frank">
<xs:complexType>
<xs:sequence>
<xs:element name="Joe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Susan" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Stuff" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
哪個看起來對我好。 然后我采用相同的模式並再次運行該工具以生成C#類。 我原本期望得到類似的東西:
[Serializable]
[XmlRoot(Namespace = "", ElementName = "Mary")]
public class Mary
{
[XmlElement("Frank")]
public Frank[] Frank { get; set; }
}
[Serializable]
public class Frank
{
[XmlElement("Joe")]
public Joe[] Joe { get; set; }
}
[Serializable]
public class Joe
{
[XmlElement("Susan")]
public Susan[] Susan { get; set; }
}
[Serializable]
public class Susan
{
[XmlElement("Stuff")]
public string Stuff { get; set; }
}
但相反,我得到了你在問題中鏈接的相同的破碎類。 所以它看起來像xsd工具中的一個bug給我。
要使其工作,您可以使用我上面制作的手工編碼類,將初始化代碼更改為:
var susan = new Susan { Stuff = "my data" };
var joe = new Joe { Susan = new Susan[] { susan } };
var frank = new Frank { Joe = new Joe[] { joe } };
var mary = new Mary { Frank = new Frank[] { frank } };
- 要么 -
另一種選擇是改變xsd。 用xs:choice
替換Frank
和Joe
元素的xs:sequence
指示符,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Mary" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Mary" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Frank">
<xs:complexType>
<xs:choice> <!-- was xs:sequence -->
<xs:element name="Joe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:choice> <!-- was xs:sequence -->
<xs:element name="Susan" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Stuff" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice> <!-- was /xs:sequence -->
</xs:complexType>
</xs:element>
</xs:choice> <!-- was /xs:sequence -->
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
使用此模式,生成的類更好:現在有一個類來表示Joe。 (為了簡潔,我在這里簡化了生成的代碼並刪除了一些屬性):
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Mary {
[System.Xml.Serialization.XmlElementAttribute("Frank", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public MaryFrank[] Items { get; set; }
}
[System.SerializableAttribute()]
public partial class MaryFrank {
[System.Xml.Serialization.XmlElementAttribute("Joe", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public MaryFrankJoe[] Items { get; set; }
}
[System.SerializableAttribute()]
public partial class MaryFrankJoe {
[System.Xml.Serialization.XmlElementAttribute("Susan", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public MaryFrankJoeSusan[] Items { get; set; }
}
[System.SerializableAttribute()]
public partial class MaryFrankJoeSusan {
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Stuff { get; set; }
}
然后設置代碼變為:
var susan = new MaryFrankJoeSusan() { Stuff = "my data" };
var joe = new MaryFrankJoe() { Items = new MaryFrankJoeSusan[] { susan } };
var frank = new MaryFrank() { Items = new MaryFrankJoe[] { joe } };
var mary = new Mary { Items = new MaryFrank[] { frank } };
我們得到了預期的輸出:
<?xml version="1.0" encoding="utf-16"?>
<Mary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Frank>
<Joe>
<Susan>
<Stuff>my data</Stuff>
</Susan>
</Joe>
</Frank>
</Mary>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.