我有一个XML文件,它代表UI中Form的内容的序列化实例。此表单由用户设计,允许其他用户填写表单。保存表单后,实例数据将序列化为数据库中的列。我正在调用这个实例数据,我需要解析它,但遇到反序列化问题 - 使用C#标准反序列化。就用户而言,这是一个表单,但实例数据实际上由“子表单”组成 - 这些是用户看到的表单所包含的表单。
以下是XML文档的格式:
<Thing>
<TheSections>
<TheSection>
<TheForms>
<TheForm name="SomeUserForm">
<Property name="BackgroundColor"/>
<Control name="GroupHeading1" type="header" hasdata="N">
<Control name="GroupHeading2" type="subheader" hasdata="N">
<Control name="TextEntry1" type="detail" hasdata="Y">
<Property name="ValueField">I want to order 3 of these</Property>
<Property name="Type">TextField</Property>
</Control>
</Control>
</Control>
</Form>
<Form>....</Form>
<Form>....</Form>
<Form>....</Form>
</TheForms>
</TheSection>
<TheSection>...</TheSection>
</TheSections>
</Thing>
如您所见,控件可以嵌套在其他控件中,属性是控件的一部分。
我有反序列化工作到表单级别,但是当我尝试反序列化控件时它会中断。
此文件中的嵌套控件对我来说并不重要 - 因为我希望获得所有控件的一个级别集合,而不管其是否为heirarchy及其所有属性。我想在控件中添加一个属性,指示如果有一个控件父控件的名称是什么 - 但除此之外,我想要一个具有控件及其属性的平面集合的对象。
有没有办法让反序列化查看这个XML,好像控件都在同一级别,无论它们的层次结构如何,只是自动反序列化所有嵌套?
感谢您的任何提示!
以下是从Visual Studio 2008中的XML文件生成的XSD(正如您将看到的那样,原始XML在每个部分上方没有表单或每个部分上方的部分 - 我在加载XML时添加了:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Thing">
<xs:complexType>
<xs:sequence>
<xs:element name="TheSection">
<xs:complexType>
<xs:sequence>
<xs:element name="TheForm">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Control">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="Group">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" name="GroupItem">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="taborder" type="xs:unsignedByte" use="required" />
<xs:attribute name="posneg" type="xs:string" use="required" />
<xs:attribute name="qualifier" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="render" type="xs:string" use="required" />
<xs:attribute name="size" type="xs:unsignedByte" use="required" />
<xs:attribute name="indent" type="xs:string" use="required" />
<xs:attribute name="newline" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Control">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" name="Property">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="hasdata" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="font" type="xs:string" use="required" />
<xs:attribute name="size" type="xs:unsignedByte" use="required" />
<xs:attribute name="indent" type="xs:string" use="required" />
<xs:attribute name="bold" type="xs:string" use="required" />
<xs:attribute name="bullet" type="xs:string" use="required" />
<xs:attribute name="newline" type="xs:string" use="required" />
<xs:attribute name="render" type="xs:string" use="required" />
<xs:attribute name="forceparagraph" type="xs:string" use="required" />
<xs:attribute name="startnewsentence" type="xs:string" use="required" />
<xs:attribute name="commadelimited" type="xs:string" use="required" />
<xs:attribute name="qualifier" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="RenderText" type="xs:string" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="hasdata" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="font" type="xs:string" use="required" />
<xs:attribute name="size" type="xs:unsignedByte" use="required" />
<xs:attribute name="indent" type="xs:string" use="required" />
<xs:attribute name="bold" type="xs:string" use="required" />
<xs:attribute name="bullet" type="xs:string" use="required" />
<xs:attribute name="newline" type="xs:string" use="required" />
<xs:attribute name="render" type="xs:string" use="required" />
<xs:attribute name="forceparagraph" type="xs:string" use="required" />
<xs:attribute name="startnewsentence" type="xs:string" use="required" />
<xs:attribute name="commadelimited" type="xs:string" use="required" />
<xs:attribute name="qualifier" type="xs:string" use="required" />
<xs:attribute name="headinglevel" type="xs:unsignedByte" use="required" />
<xs:attribute name="normalgroup" type="xs:string" use="required" />
<xs:attribute name="renderwithdata" type="xs:string" use="required" />
<xs:attribute name="grouptype" type="xs:string" use="required" />
<xs:attribute name="label" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="font" type="xs:string" use="required" />
<xs:attribute name="size" type="xs:unsignedByte" use="required" />
<xs:attribute name="indent" type="xs:string" use="required" />
<xs:attribute name="bold" type="xs:string" use="required" />
<xs:attribute name="bullet" type="xs:string" use="required" />
<xs:attribute name="newline" type="xs:string" use="required" />
<xs:attribute name="forceparagraph" type="xs:string" use="required" />
<xs:attribute name="startnewsentence" type="xs:string" use="required" />
<xs:attribute name="commadelimited" type="xs:string" use="required" />
<xs:attribute name="qualifier" type="xs:string" use="required" />
<xs:attribute name="render" type="xs:string" use="required" />
<xs:attribute name="rendercount" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="id" type="xs:unsignedByte" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="render" type="xs:string" use="required" />
<xs:attribute name="font" type="xs:string" use="required" />
<xs:attribute name="size" type="xs:unsignedByte" use="required" />
<xs:attribute name="indent" type="xs:string" use="required" />
<xs:attribute name="bold" type="xs:string" use="required" />
<xs:attribute name="bullet" type="xs:string" use="required" />
<xs:attribute name="newline" type="xs:string" use="required" />
<xs:attribute name="forceparagraph" type="xs:string" use="required" />
<xs:attribute name="rendercount" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
以下是我用于反序列化的类的示例:
[ XmlRoot("Section")
]
public class MySectionData
{
private MyFormData[] _Forms = new MyFormData[0];
[ XmlArray("Forms")
]
[ XmlArrayItem("Form")
]
public MyFormData[] Forms
{
set
{
this._Forms = value;
}
get
{
return this._Forms;
}
}
[ XmlAttribute("id")
]
public String Id
{
set;
get;
}
[XmlAttribute("name")
]
public String name
{
set;
get;
}
}
答案 0 :(得分:2)
您可能会花费大量时间和精力来确定XML序列化的细微差别并尝试构建类,以便它们可以或多或少地使用此格式。不过,我认为这更容易:
创建一个可序列化为XML的Control
类。创建一个可序列化的类,其中包含Control
个对象的集合,填充它并对其进行序列化。现在您知道XML反序列化器对此类需要什么样的输入。
编写一个XSLT转换,将您拥有的输入XML转换为您需要的输入XML。
答案 1 :(得分:0)
您是否尝试过使用xsd.exe根据架构生成类?如果“控件”最终被嵌套,你仍然可以编写一个函数来“展平”它们并返回整个集合,对吗?
它位于:C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Bin \ NETFX 4.0 Tools \