以编程方式向Xml架构添加注释和换行符

时间:2013-09-17 07:56:24

标签: c# xml xsd

我正在使用System.Xml.Schema.XmlSchema类以编程方式创建xml架构,但它的大小变得难以理解。有没有办法输出评论和换行符?

这是我的代码的一部分:

XmlSchema schema = new XmlSchema();
schema.TargetNamespace = "my-ns";
schema.Namespaces.Add("","my-ns");

// do stuff here
...

// and then add some comments like <!-- here comes the second part -->
schema.Items.Add(COMMENTLINE); // How does this work? A line break will be fine too.

// and do more stuff
...

// then write it to a file
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

using(XmlWriter tw = XmlTextWriter.Create(filename, settings)
{
    schema.Write(tw);
}

期望的输出:

<xs:element>
  <!-- comment my stuff -->
  <xs:sequence>
    <xs:element/>
  </xs:sequence>
</xs:element>

1 个答案:

答案 0 :(得分:0)

它是一个XML文档。只需使用漂亮的打印机。