![](/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.