【发布时间】:2015-03-03 18:35:50
【问题描述】:
我有一个架构生成器,它使用 XmlSchema 来构建基于 EF 模型的 xml。
我的问题是,我想以人类可读的格式返回 XMLschema,以便我可以将其以 HTML 格式显示给最终用户。
我尝试过使用 toString(),但这不起作用。返回我的 XMLSchema 的最佳方式是什么?
xml 格式正确,我想我只是不知道如何将它返回到前端,所以我可以将它包装在突出显示中
代码如下:
public ActionResult ResourceXML(int id)
{
Resource res = db.Resources.Find(id);
res.Properties = db.ResourceProperties.Where(c => c.ResourceId == id).ToList();
XmlSchema schema = new XmlSchema();
XmlSchemaElement resource = new XmlSchemaElement();
schema.Namespaces.Add("hrr", "http://schema.hrr.com/hrr");
schema.Namespaces.Add("xs", "http://www.w3.org/2001/XMLSchema");
schema.TargetNamespace = "http://www.w3schools.com";
resource.Name = res.Name;
resource.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
resource.Id = ""+res.Id;
//annotation
XmlSchemaAnnotation resourceAnno = new XmlSchemaAnnotation();
resource.Annotation = resourceAnno;
//documentation
XmlSchemaDocumentation docs = new XmlSchemaDocumentation();
resourceAnno.Items.Add(docs);
docs.Markup = TextToNodeArray(""+res.Description);
//complex type
XmlSchemaComplexType complexType = new XmlSchemaComplexType();
//sequence
XmlSchemaSequence sequence = new XmlSchemaSequence();
complexType.Particle = sequence;
foreach (var property in res.Properties)
{
XmlSchemaElement element = new XmlSchemaElement();
element.Name = property.Name;
element.SchemaTypeName = new XmlQualifiedName(GetTheType(property), "http://www.w3.org/2001/XMLSchema");
element.Id = property.Id+"";
if (property.Required)
{
element.MinOccurs = 1;
}
else
{
element.MinOccurs = 0;
}
if (property.List)
{
element.MaxOccursString = "unbounded";
}
else
{
element.MaxOccursString = "1";
}
//annotation
XmlSchemaAnnotation elemAnno = new XmlSchemaAnnotation();
element.Annotation = elemAnno;
//documentation
XmlSchemaDocumentation elemDoc = new XmlSchemaDocumentation();
resourceAnno.Items.Add(elemDoc);
//No property data
elemDoc.Markup = TextToNodeArray("");
sequence.Items.Add(element);
}
schema.Items.Add(resource);
schema.Version = "" + res.Version.Number;
//schema.Write(stream:);
return File(stream, "text/xml");
}
【问题讨论】:
-
我使用 HtmlTextWriter 将我的 XML 输出到 HTML。如果您希望使用 HTML 标记更好地控制 XML 的呈现方式,则可以这样做。
标签: c# xml asp.net-mvc entity-framework model-view-controller