this과 같은 xsd
파일을 만들려고합니다.XML 스키마에서 중첩 된 XmlSchemaComplexType 만들기
나는이 잘 작동하지 않습니다이
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();
을 사용하고 있습니다. 내가 두 번째 XmlSchemaComplexType
다음 가 작동하고 벌금을 사용하고 있지 않다 때 는 예외 Object reference not set to an instance of an object
는 compiledSchema.Write(writer, nsmgr);
에서 발생했습니다. 여기 같은 방법으로 동일한 주먹을 사용하고 있습니다 XmlSchemaComplexType
을 추가하는 데 사용하고 있지만 그것이 작동하지 않는 두 번째 시간.
제발. compiledSchema
이 schemaSet.Schemas()
이 스키마가 고장이기 때문이다 schemaSet.Add(schema)
실패 때문이다 빈 수집,이기 때문이다, 널 (null)이기 때문에