开发者

Output XML to ASP.NET TextBox with formatting using XmlWriter?

I am building an XML string programatically using the XmlWriter ...

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() & "&开发者_如何转开发lt;/pre>"

I want to be able to output the XML to a TextBox control on a ASP.NET webform. When I do output the XML I don't get any line breaks or indentation in the XML. The XmlWriter does not seem to have any properties to set formatting. Is there anyway to retain formatting and indentation? Thank you.


Use a XmlWriterSettings class instance to specify the use of indentations and line breaks and then pass it into the xmlWriter's Create method. See the documentation here.


try this

  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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜