在 XML 架构中创建嵌套的 XmlSchemaComplexType
Create nested XmlSchemaComplexType in XML schema
我想创建一个 xsd
文件,例如 this。
我正在使用这个
XmlSchema schema = new XmlSchema();
schema.Id = "SHP_CAHPS_HOSPICE_DATA";
XmlSchemaElement elementData = new XmlSchemaElement();
schema.Items.Add(elementData);
elementData.Name = "SHP_CAHPS_HOSPICE_DATA";
XmlSchemaComplexType complexType = new XmlSchemaComplexType();
elementData.SchemaType = complexType;// This is same as below. It's working fine but next is not working.
XmlSchemaSequence sequence = new XmlSchemaSequence();
complexType.Particle = sequence;
// here some elements
XmlSchemaElement surveydataEle = new XmlSchemaElement();
sequence.Items.Add(surveydataEle);
surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA";
surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
surveydataEle.MinOccurs = 1;
surveydataEle.MaxOccurs = 2000;
XmlSchemaComplexType complexType1 = new XmlSchemaComplexType();
surveydataEle.SchemaType = complexType1;
XmlSchemaSequence sequence1 = new XmlSchemaSequence();
complexType1.Particle = sequence1;
Type type = patient.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
string name = property.Name;
XmlSchemaElement element = new XmlSchemaElement();
sequence1.Items.Add(element);
element.Name = name;
element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
element.MinOccurs = 0;
}
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
schemaSet.Add(schema);
schemaSet.Compile();
XmlSchema compiledSchema = null;
foreach (XmlSchema schema1 in schemaSet.Schemas())
{
compiledSchema = schema1;
}
Random r = new Random();
int random = r.Next();
string filePath = "D:\" + random + ".xsd";
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
//Stream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(filePath, new UTF8Encoding());
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
compiledSchema.Write(writer, nsmgr); // Exception is occurring here
writer.Flush();
writer.Close();
这不能正常工作。
在 compiledSchema.Write(writer, nsmgr);
发生异常 Object reference not set to an instance of an object
当我不使用秒 XmlSchemaComplexType
then
它工作正常。在这里,我使用相同的方式添加 XmlSchemaComplexType
,这是我使用的拳头,但第二次它不起作用。
请推荐我。
NullReferenceException
被抛出是因为 compiledSchema
是空的,这是因为 schemaSet.Schemas()
是一个空集合,这是因为 schemaSet.Add(schema)
失败了,这是因为你的架构已损坏。
你的模式被破坏的原因是你声明 CAHPS_HOSPICE_SURVEY_DATA
既是一个字符串又是一个复杂类型,从你的 schema
中转储 XML 可以看出变量:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="SHP_CAHPS_HOSPICE_DATA" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="SHP_CAHPS_HOSPICE_DATA">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="2000" name="CAHPS_HOSPICE_SURVEY_DATA" type="xsd:string">
<xsd:complexType>
<xsd:sequence>
你在这里声明它是一个字符串使用SchemaTypeName
:
surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
然后您可以在此处使用 SchemaType
:
将其声明为复杂类型
surveydataEle.SchemaType = complexType1;
解决方法是删除第一个声明:
XmlSchemaElement surveydataEle = new XmlSchemaElement();
sequence.Items.Add(surveydataEle);
surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA";
// surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); <-- Deleted this line.
surveydataEle.MinOccurs = 1;
surveydataEle.MaxOccurs = 2000;
XmlSchemaComplexType complexType1 = new XmlSchemaComplexType();
surveydataEle.SchemaType = complexType1;
我想创建一个 xsd
文件,例如 this。
我正在使用这个
XmlSchema schema = new XmlSchema();
schema.Id = "SHP_CAHPS_HOSPICE_DATA";
XmlSchemaElement elementData = new XmlSchemaElement();
schema.Items.Add(elementData);
elementData.Name = "SHP_CAHPS_HOSPICE_DATA";
XmlSchemaComplexType complexType = new XmlSchemaComplexType();
elementData.SchemaType = complexType;// This is same as below. It's working fine but next is not working.
XmlSchemaSequence sequence = new XmlSchemaSequence();
complexType.Particle = sequence;
// here some elements
XmlSchemaElement surveydataEle = new XmlSchemaElement();
sequence.Items.Add(surveydataEle);
surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA";
surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
surveydataEle.MinOccurs = 1;
surveydataEle.MaxOccurs = 2000;
XmlSchemaComplexType complexType1 = new XmlSchemaComplexType();
surveydataEle.SchemaType = complexType1;
XmlSchemaSequence sequence1 = new XmlSchemaSequence();
complexType1.Particle = sequence1;
Type type = patient.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
string name = property.Name;
XmlSchemaElement element = new XmlSchemaElement();
sequence1.Items.Add(element);
element.Name = name;
element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
element.MinOccurs = 0;
}
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
schemaSet.Add(schema);
schemaSet.Compile();
XmlSchema compiledSchema = null;
foreach (XmlSchema schema1 in schemaSet.Schemas())
{
compiledSchema = schema1;
}
Random r = new Random();
int random = r.Next();
string filePath = "D:\" + random + ".xsd";
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
//Stream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(filePath, new UTF8Encoding());
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
compiledSchema.Write(writer, nsmgr); // Exception is occurring here
writer.Flush();
writer.Close();
这不能正常工作。
在 compiledSchema.Write(writer, nsmgr);
Object reference not set to an instance of an object
当我不使用秒 XmlSchemaComplexType
then
它工作正常。在这里,我使用相同的方式添加 XmlSchemaComplexType
,这是我使用的拳头,但第二次它不起作用。
请推荐我。
NullReferenceException
被抛出是因为 compiledSchema
是空的,这是因为 schemaSet.Schemas()
是一个空集合,这是因为 schemaSet.Add(schema)
失败了,这是因为你的架构已损坏。
你的模式被破坏的原因是你声明 CAHPS_HOSPICE_SURVEY_DATA
既是一个字符串又是一个复杂类型,从你的 schema
中转储 XML 可以看出变量:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="SHP_CAHPS_HOSPICE_DATA" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="SHP_CAHPS_HOSPICE_DATA">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="2000" name="CAHPS_HOSPICE_SURVEY_DATA" type="xsd:string">
<xsd:complexType>
<xsd:sequence>
你在这里声明它是一个字符串使用SchemaTypeName
:
surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
然后您可以在此处使用 SchemaType
:
surveydataEle.SchemaType = complexType1;
解决方法是删除第一个声明:
XmlSchemaElement surveydataEle = new XmlSchemaElement();
sequence.Items.Add(surveydataEle);
surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA";
// surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); <-- Deleted this line.
surveydataEle.MinOccurs = 1;
surveydataEle.MaxOccurs = 2000;
XmlSchemaComplexType complexType1 = new XmlSchemaComplexType();
surveydataEle.SchemaType = complexType1;