我正在使用XmlWriter以编程方式构建XML字符串...
sb = New StringBuilder()
writer = XmlWriter.Create(sb)
writer.WriteStartDocument()
writer.WriteStartElement("root")
writer.WriteStartElement("element1")
writer.WriteAttributeString("myattribute", "myvalue")
writer.WriteEndElement()
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Flush()
myTextBox.text = "<pre>" & Return sb.ToString() & "</pre>"
我希望能够将XML输出到ASP.NET webform上的TextBox控件。当我输出XML时,我没有在XML中获得任何换行符或缩进。 XmlWriter
似乎没有任何属性来设置格式。反正是否保留格式和缩进?谢谢。
答案 0 :(得分:3)
使用XmlWriterSettings类实例指定缩进和换行符的使用,然后将其传递给xmlWriter的Create方法。请参阅文档here。
答案 1 :(得分:0)
试试这个
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.Indented;
// Write Xml Declaration with version 1.0
writer.WriteStartDocument();
// Write Xml Document Root Element
writer.WriteStartElement("Product");
// Write attribute name and value to current Element
writer.WriteAttributeString("ProductID", "01");
// Write Xml Element with Name and Inner Text
writer.WriteElementString("ProductName", "P-Name");
writer.WriteElementString("ProductQuantity","P-Quantity");
writer.WriteElementString("ProductPrice", "P-Price");
// Write Product Element closing Tag
writer.WriteEndElement();
// Write End of Document
writer.WriteEndDocument();
// Flush and Close Writer
writer.Flush();
textBox1.Text = sw.ToString();